Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ember Data: (Advanced) Patterns

Paul Chavard
September 20, 2013

Ember Data: (Advanced) Patterns

Slides for my talk from EmberFest 2013

Paul Chavard

September 20, 2013
Tweet

More Decks by Paul Chavard

Other Decks in Programming

Transcript

  1. I am going to show you some useful stuff that

    actualy works (most of the time) in Ember Data
  2. Filters Dynamic Filter var IndexRoute = Ember.Route.extend ({ model: function

    () { return this.store.filter(’post ’, {}, Ember.K); } }); var IndexController = Ember. ArrayController .extend ({ searchText : ’’, searchTextDidChange : function () { var searchText = this.get(’searchText ’). toLowerCase (); var filterFunction = Ember.isEmpty(searchText ) ? Ember.K : function(record) { return record.get(’title ’). toLowerCase () .match( searchText ); }; this.set(’content. filterFunction ’, filterFunction ); }. observes(’searchText ’) });
  3. Batch Saves Save Multiple Models Ember.RSVP.all(post.get(’comments ’). invoke(’save ’)) .then(function

    () { alert(’All Saved!’); }, function(reason) { alert(’There was an error because ’ + reason ); }); Save Parent Before Child post.save (). then(function () { return comment.save (); }). then(function () { alert(’All Saved!’); }, function(reason) { alert(’There was an error because ’ + reason ); });
  4. Unload Records Unload Single Record this.store.find(’post ’, 1). unloadRecord ();

    Unload Record with its Relationships var Post = DS.Model.extend ({ unloadRecord : function () { this._super (); this. eachRelationship (function(key , relationship ) { if ( relationship .kind === ’hasMany ’) { this.get(key ). toArray (). invoke(’unloadRecord ’); } else { this.get(key ). unloadRecord (); } }, this ); } }); this.store.find(’post ’, 1). unloadRecord ();