Skip to content

Instantly share code, notes, and snippets.

@pjvds
Created September 24, 2014 13:59
Show Gist options
  • Save pjvds/e7c2ccc0f28745a8f06f to your computer and use it in GitHub Desktop.
Save pjvds/e7c2ccc0f28745a8f06f to your computer and use it in GitHub Desktop.
Profile controller creates a like-button child components and passes values via params so that the like-button can build a model out of it. This works perfectly on the server, the model is available when rendering the view of the like-button. But it never gets dehydrated on the client.
define(["lazoCtl"], function(Ctl) {
"use strict";
return Ctl.extend({
index: function(options) {
var self = this;
// create model passed on the params we get passed in.
self.createModel("like-button",
{
ownerId: self.ctx.params.ownerId,
mediaId: self.ctx.params.mediaId,
isLiked: self.ctx.params.isLiked
},
{
persist: false, // do not call backend, we also tried to use loadModel and set sync to false
success: function(model) {
// this model is created, and the view on the server renders correctly, but we loose this context in the client. it is never rehydrated on the client correctly.
self.ctx.models.like = model;
options.success("index");
},
error: function() {
options.error("index");
}
});
}
});
});
define(["lazoCtl"], function(Ctl) {
"use strict";
return Ctl.extend({
index: function(options) {
var self = this;
this.loadModel("profile", {
params: self.ctx.params,
success: function(model) {
self.profileLoaded(model, options);
},
error: function(err) {
options.error(err);
}
});
},
// called when the profile model is loaded successfully
profileLoaded: function(model, options) {
var self = this;
self.ctx.models.profile = model;
// add like-button for portrait photo.
this.addChild("like-button-cmp", "like-button", {
params: {
ownerId: model.id,
mediaId: model.portrait.id,
isLiked: model.portraitLiked
},
success: function() {
options.success("index"); // load the index view
},
error: function(err) {
options.error(err);
}
});
// todo: add like-buttons for all photo in the model.album.
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment