zoukankan      html  css  js  c++  java
  • Backbone的id

    id

    在model.attributes中,需要用户自行定义,可不定义,获取方法:model.get('id')

    cid

    collection中每个model都有的属性,由backbone自动生成,获取方法:model.cid 

    idAttribute

    在model中,用于指定使用model.attributes中哪个键作为id,默认情况下是model.attributes.id,需要用户自行定义,可不定义,获取方法:model.idAttribute

     

    当使用set进行智能判断操作时,只有idAttribute指定的键相同的情况会把该操作视为change

     

    DEMO

                   

     1                 var TodoCollection = new Backbone.Collection();
     2 
     3                 TodoCollection.add([
     4 
     5                     {id:1,title: 'go to a', completed:true},
     6 
     7                     {title: 'go to b', completed:false},
     8 
     9                     {title: 'go to c', completed:false},
    10 
    11                 ]);
    12 
    13                 TodoCollection.on('add', function(model){
    14 
    15                     console.log('add:' + model.get('title'));
    16 
    17                 });
    18 
    19                 TodoCollection.on('remove', function(model){
    20 
    21                     console.log('remove:' + model.get('title'));
    22 
    23                 });
    24 
    25                 TodoCollection.on('change', function(model){
    26 
    27                     console.log('change:' + model.get('title'));
    28 
    29                 });
    30 
    31                 TodoCollection.set([
    32 
    33                     {id:1,title: 'go to a', completed:false},
    34 
    35                     {title: 'go to b', completed:false},
    36 
    37                     {title: 'go to c', completed:false},
    38 
    39                 ]);
    View Code

    该代码的执行结果是

     

    1 change:go to a 
    2 remove:go to b 
    3 remove:go to c 
    4 add:go to b 
    5 add:go to c 
    View Code
  • 相关阅读:
    iOS静态库的制作
    iOS视频硬件编解码H264
    cocoapods私有库创建
    Mac OS 下基于XAMPP的Phabricator 安装
    OpenGL01(你好,窗口)
    GPUImage类注解
    cocoapods公有库创建
    GCD-调度组
    对初级程序员的思考
    Swift学习(4懒加载、计算型属性、反射机制)
  • 原文地址:https://www.cnblogs.com/willkan/p/3387108.html
Copyright © 2011-2022 走看看