Slide 1

Slide 1 text

Ember Data (Advanced) Patterns Paul Chavard Capitaine Train August 30, 2013

Slide 2

Slide 2 text

Capitaine Train capitainetrain.com • 55 controllers • 22 models

Slide 3

Slide 3 text

I am going to show you some useful stuff that actualy works (most of the time) in Ember Data

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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 ();

Slide 7

Slide 7 text

Ember Extention

Slide 8

Slide 8 text

The End