Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Views cleanup #216

Merged
merged 3 commits into from
Feb 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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