Created
May 25, 2014 02:51
-
-
Save tobobo/6c9563fa6068f17cb67c to your computer and use it in GitHub Desktop.
Loading a partial model in ember
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App = Ember.Application.create({}); | |
| App.Router.map(function () { | |
| this.route('something', { | |
| path: 'something/:someId' | |
| }) | |
| }); | |
| App.IndexRoute = Ember.Route.extend({ | |
| model: function () { | |
| return [{ | |
| firstName: 'Kris', | |
| lastName: 'Selden' | |
| }, { | |
| firstName: 'Luke', | |
| lastName: 'Melia' | |
| }, { | |
| firstName: 'Formerly Alex', | |
| lastName: 'Matchneer' | |
| }]; | |
| } | |
| }); | |
| App.IndexController = Ember.ArrayController.extend({ | |
| newModel: [{ | |
| firstName: 'what', | |
| lastName: 'fun' | |
| }] | |
| }); | |
| App.BaseRoute = Ember.Route.extend({ | |
| isPartialModel: function (model) { | |
| return false; | |
| }, | |
| updateModel: function (model) { | |
| return this.replaceWith(this.get('routeName'), model); | |
| }, | |
| afterModel: function (model) { | |
| console.log('after model parent'); | |
| route = this; | |
| if (route.isPartialModel(model)) { | |
| model.set('isLoading', true); | |
| newModel = this.model(); | |
| if (!newModel.then) { | |
| return route.updateModel(newModel); | |
| } else { | |
| console.log('updating model'); | |
| return newModel.then(function (model) { | |
| return route.updateModel(model); | |
| }); | |
| } | |
| } else { | |
| console.log('model is good'); | |
| return Ember.RSVP.resolve(model); | |
| } | |
| } | |
| }); | |
| App.SomethingRoute = App.BaseRoute.extend({ | |
| isPartialModel: function (model) { | |
| return !model.get('firstObject.interests'); | |
| }, | |
| model: function () { | |
| return new Ember.RSVP.Promise(function (resolve, reject) { | |
| setTimeout(function () { | |
| resolve(Ember.ArrayProxy.create({ | |
| content: [{ | |
| firstName: 'what', | |
| lastName: 'fun', | |
| interests: 'baseball' | |
| }] | |
| })); | |
| }, 1000); | |
| }); | |
| }, | |
| afterModel: function () { | |
| console.log('after model outside'); | |
| this._super.apply(this, arguments).then(function () { | |
| console.log('args', arguments); | |
| console.log('after model'); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment