Sea guy
Sea guy

Reputation: 178

Remove all item of Model using Emberjs

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.

Upvotes: 1

Views: 1545

Answers (1)

intuitivepixel
intuitivepixel

Reputation: 23322

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.

Upvotes: 2

Related Questions