Remove all item of Model using Emberjs

Asked By: Anonymous

Define Model

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'}
]; 

Define Controller

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"
        });
    }   
}); 

I am going to remove all itme and add some one to model object in changeMarkets function using emberjs.
I’d like to know how to remove all itme of Model.


Solution

Answered By: Anonymous

The short see here for a working example: http://jsbin.com/eyojit/5/edit

The long: To remove all the items from the controller content property you could use the built-in method clear(), this will be more performant and your bindings will work correctly. Then to set new items you could do it like this, here the relevant code:

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"
      })
    ]);
  }   
});

Hope it helps.


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno