Skip to content

Instantly share code, notes, and snippets.

@tobobo
Created May 25, 2014 02:51
Show Gist options
  • Select an option

  • Save tobobo/6c9563fa6068f17cb67c to your computer and use it in GitHub Desktop.

Select an option

Save tobobo/6c9563fa6068f17cb67c to your computer and use it in GitHub Desktop.
Loading a partial model in ember
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