Skip to content

Commit

Permalink
Merge pull request #216 from inaes-tic/views-cleanup
Browse files Browse the repository at this point in the history
Views cleanup
  • Loading branch information
josx committed Feb 21, 2014
2 parents 5aae2e1 + c515b65 commit cbde7cc
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 258 deletions.
12 changes: 12 additions & 0 deletions public/js/views/masterview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ window.MasterView = Backbone.View.extend({
initialize: function() {
var self = this;

_.extend(this, Backbone.Events);

// This allows us to hook before and after rendering happens.
// XXX: keep the _.bindAll, otherwise 'this' will point to the
// global object inside render()
_.bindAll(this, 'render');
this.render = _.wrap(this.render, function(render) {
this.trigger('prerender');
render();
this.trigger('postrender');
});

// This allows the view to be extended without losing bindings
this.events = _.extend({}, this.events, this.genericEvents);
this.delegateEvents();
Expand Down
48 changes: 31 additions & 17 deletions public/js/views/mediadetails.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
window.MediaView = function (options) {
var self = this;
options = options || {};
window.MediaView = MasterView.extend({
el: "#content",
initialize: function (options) {
// Parent initialize
MasterView.prototype.initialize.apply(this, arguments);

this.el = $('#content');
this.options = options || {};
this.el = options.el || $('#content');
_.bindAll(this, 'render', 'canNavigateAway');

this.render = function() {
this.model = options.model;
if (this.model !== undefined) {
this.render();
} else {
this.model = Media.Model.findOrCreate({ _id: options.id });
this.model.fetch({
success: this.render,
});
}
},

render: function() {
var self = this;
self.el.html(template.mediaview(self.model.toJSON()));

self.view_model = kb.viewModel(self.model);
Expand All @@ -16,16 +32,14 @@ window.MediaView = function (options) {
};

ko.applyBindings(self.view_model, self.el[0]);
}

this.model = options['model'];
if (this.model !== undefined) {
this.render();
} else {
this.model = Media.Model.findOrCreate({ _id: options["id"], });
this.model.fetch({
success: this.render,
});
}
}
},

canNavigateAway: function (options) {
var self = this;
kb.release(self.view_model);
// Clear element
ko.cleanNode(self.el[0]);
self.el.html('');
options['ok']();
},
});
Loading

0 comments on commit cbde7cc

Please sign in to comment.