2017年12月29日 星期五

GreenBackbone view 的 ajax(template | component)


--------------------------------------------------------------------------
// component的例子
<div>
    {{x}}
</div>
<script>
    // 把上面的 html內容傳進 fun
    function(template) {
        return Backbone.BForView.extend({
            templateContent: template
        });
    }
</script>
--------------------------------------------------------------------------
// template的例子
<div>
    {{x}}
</div>
<script>
    function(view) {
          // template mounted
    }
</script>
<script>
    function(view) {
          // template removed    }
</script>
 --------------------------------------------------------------------------
// template 改進
<div>
    {{x}}
</div>
<script>
  
    function() {
         return {
               mounted: fun(view),
               unmouted: fun(view)
              }
    }
</script>

 --------------------------------------------------------------------------

2017年12月23日 星期六

Backbone.Reactive 重要的例子

var d_1 = [{
            age: 12,
            selected: true
        }, {
            age: 1,
            selected: false
        }, {
            age: 2,
            selected: false
        }];
        var d_2 = Backbone.Reactive(d_1, {
            link: function(parent, child) {

                parent.listenTo(child, 'all', function(e, event, options) {
                    debugger;
                    var info = 'parent => ' + event + '|' + JSON.stringify(arguments);

                    for (var i = 0; i < this.value.length; i++) {
                        debugger;
                        var v = this.value[i];
                        var ob = v._bc_ob_;

                        if (v._bc_ob_.cid !== e.target.cid) {
                            ob.triggerOptions = {
                                exclude: [this] // 防止事件無窮回圈
                            }
                            v.selected = false;
                        }
                    }

                    $('#msg_1').html(JSON.stringify(this));
                })
            }
        });
        /*--------------------------*/

        d_2._bc_ob_.on('change', function() {
            var info = 'parent => ' + event + '|' + JSON.stringify(arguments);
            alert(info);
        });

        function set() {

            var index = Math.floor(Math.random() * d_2.length);
            index = (index >= d_2.length ? (d_2.length - 1) : index);
            d_2[index].selected = true;
        }

2017年12月22日 星期五

javascript Map

Properties   
    Map.prototype.size   

Methods
    Map.prototype.clear()

    Map.prototype.delete()

    Map.prototype.entries()

    Map.prototype.forEach()

    Map.prototype.get()

    Map.prototype.has()

    Map.prototype.keys()

    Map.prototype.set()

    Map.prototype.values()

    Map.prototype[@@iterator]()
-----------------------------------------------------------------

範例

區段

使用 Map 物件

區段

var myMap = new Map();

var keyString = 'a string',
    keyObj = {},
    keyFunc = function() {};

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, 'value associated with keyObj');
myMap.set(keyFunc, 'value associated with keyFunc');

myMap.size; // 3

// getting the values
myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

myMap.get('a string');   // "value associated with 'a string'"
                         // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFunc !== function () {}

使用 NaN 作為 Map 的鍵

區段

NaN can also be used as a key. Even though every NaN is not equal to itself (NaN !== NaN is true), the following example works because NaNs are indistinguishable from each other:
var myMap = new Map();
myMap.set(NaN, 'not a number');

myMap.get(NaN); // "not a number"

var otherNaN = Number('foo');
myMap.get(otherNaN); // "not a number"

透過 for..of 迭代 Maps

區段

Maps can be iterated using a for..of loop:
var myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');
for (var [key, value] of myMap) {
  console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one

for (var key of myMap.keys()) {
  console.log(key);
}
// 0
// 1

for (var value of myMap.values()) {
  console.log(value);
}
// zero
// one

for (var [key, value] of myMap.entries()) {
  console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one

透過 forEach() 迭代 Maps

區段

Maps can be iterated using the forEach() method:
myMap.forEach(function(value, key) {
  console.log(key + ' = ' + value);
});
// Will show 2 logs; first with "0 = zero" and second with "1 = one"

與 Array 物件關聯

區段

var kvArray = [['key1', 'value1'], ['key2', 'value2']];

// Use the regular Map constructor to transform a 2D key-value Array into a map
var myMap = new Map(kvArray);

myMap.get('key1'); // returns "value1"

// Use the Array.from function to transform a map into a 2D key-value Array
console.log(Array.from(myMap)); // Will show you exactly the same Array as kvArray

// Or use the keys or values iterators and convert them to an array
console.log(Array.from(myMap.keys())); // Will show ["key1", "key2"]

2017年12月11日 星期一

drupal 簡單 model_example

<?php

// $Id$

function gt_site_menu() {

    $items['site/%'] = array(
        'title' => 'test',
        'page callback' => 'goPage',
        'access callback' => true
    );

    return $items;
}

function goPage() {
    $res = theme('home', array());
    return $res;
}

function gt_site_theme() {
    // 取得 gt_site 該 model 的 path
    $path = drupal_get_path('module', 'gt_site');
    $path .= '/themes';

    return array(
        'home' => array(
            'template' => 'home',
            'path' => $path)
    );
}

/**
 * Implements hook_help().
 */

function gt_site_help($path, $arg) {

    $res = array();

    foreach ($_GET as $key => $value) {
        $res[] = sprintf('%s => %s', $key, $value);
    }
   
    $res[] = $path = drupal_get_path('module', 'gt_site');
   
    $res = implode(' | ', $res);

    return t($res);
}

2017年12月5日 星期二

js 筆記

let x = 1;

      y = (++x, ++x);
      console.log(y);

y=3
-----------------------------------------------------------------------------
js 基本類型

boolean, number, string, undefined
null is object
-----------------------------------------------------------------------------

js一些預設方法

 

string.slice(s, e), array.slice(s, e) => slice(切片) 

string.split(符號) 分割成 array => split (分裂)

array.splice(s, l, data_1, data_2)=>splice (拼接)



string.match() => //正則是否用/g差別很大,/g將不顯示捕獲組,但會將符合的化為 array
regular.exec()