zoukankan      html  css  js  c++  java
  • [Backbone]6. Collections.

    Define a collection:

    var AppointmentList = Backbone.Collection.extend({model: Appointment});

    RESET the json:

    var appointments = new AppointmentList();
    var json = [
      {title: 'Back pain'},
      {title: 'Dry mouth'},
      {title: 'Headache'} 
    ];
    appointments.reset(json);

     Get url and fetch json data:

    var AppointmentList = Backbone.Collection.extend({
      url: '/appointments',
      model: Appointment
    });
    var appointments = new AppointmentList();
    
    appointments.fetch();

    This Dr. Goodparts does not trust us when we tell him we are successfully loading data from the server into our collection.

    To prove him wrong, display an alert with the number of items in the collection by listening for the reset event.

    var appointments = new AppointmentList();
    appointments.fetch();
    appointments.on('reset', function(){
        alert(appointments.length);
    });

    Wouldn't ya know, our users don't like getting alerts every time we fetch new data for our collection.

    Update the fetch call below to not fire the reset event.

    //By default, when fetch or add model, the reset event will be triggered.
    //TO disable the reset event, can use 
    appointments.fetch({silent: true});

    Use an event listener to log to the console the model's title anytime a model is added to the appointments collection.

    var appointments = new AppointmentList();
    appointments.on('add', function(appointment){
        console.log(appointment.get('title'));
    });

    There are a lot of appointments in our collection and Dr. Goodparts wants a list of all appointment titles so he can arrange his equipment for the day.

    Use the map iteration function to return an array of appointment titles and assign to the titles variable.

    var titles = new Array();
    appointments.map(function(appoinment){
        titles.push(appoinment.get('title'));
    });
  • 相关阅读:
    6.基础控件
    5.对话框组合
    4.对话框程序
    3. MFC原理介绍
    三星曲面显示器
    笔记本光驱接口怎么外接台式机硬盘?
    笔记本usb外接3.5寸sata台式机硬盘, 用mhdd检测不到,怎么处理?
    MHDD检测不到硬盘的解决办法
    MHDD检测电脑硬盘坏道
    一个指向指针函数的函数指针
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3887500.html
Copyright © 2011-2022 走看看