使用Emberjs删除所有Model项目

时间:2013-07-19 08:32:53

标签: javascript jquery model ember.js

定义模型

App.Store = DS.Store.extend({
   revision: 12,
   adapter: DS.FixtureAdapter
});

App.Markets = DS.Model.extend({
    name: DS.attr("string"),
    created: DS.attr("string")
});

App.Markets.FIXTURES = [
    {id:1, name:"name1", created:'2013-1-1'},
    {id:2, name:"name2", created:'2013-1-2'},
    {id:3, name:"name3", created:'2013-1-3'}
]; 

定义控制器

App.MarketsController = Ember.ArrayController.extend({
    changeMarkets:function(marketer_id){
        // remove all item of App.Markets Model
        //-------

        // add new item in App.Markets Model
        App.Sources.createRecord({
            id: 100,
            name: "new name1",
            created: "2014-2-1"
        });

        App.Sources.createRecord({
            id: 200,
            name: "new name2",
            created: "2014-2-2"
        });

        App.Sources.createRecord({
            id: 300,
            name: "new name3",
            created: "2014-2-3"
        });
    }   
}); 

我将删除所有itme并使用emberjs在changeMarkets函数中添加一个模型对象。 我想知道如何删除Model的所有内容。

1 个答案:

答案 0 :(得分:2)

简短请点击此处查看工作示例:http://jsbin.com/eyojit/5/edit

long:要从控制器content属性中删除所有项目,您可以使用内置方法clear(),这将更高效,您的绑定将工作正常。然后设置新项目,你可以这样做,这里是相关的代码:

App.MarketsController = Ember.ArrayController.extend({
  changeMarkets:function(marketer_id){
    // remove all item of App.Markets Model
    this.get('content').clear();

    // add new item in App.Markets Model
    this.set('content', [
      App.Markets.createRecord({
        id: 100,
        name: "new name1",
        created: "2014-2-1"
      }),
      App.Markets.createRecord({
        id: 200,
        name: "new name2",
        created: "2014-2-2"
      }),
      App.Markets.createRecord({
        id: 300,
        name: "new name3",
        created: "2014-2-3"
      })
    ]);
  }   
});

希望它有所帮助。