-
-
Save cuppster/3518966 to your computer and use it in GitHub Desktop.
Updating Views with Events, Models and Rivets
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
// ## Header View | |
// | |
var HeaderView = Backbone.View.extend({ | |
// model backing the view | |
model: new (Backbone.Model.extend({ | |
defaults: { | |
hasUpdate: false, | |
} | |
})), | |
initialize: function() { | |
var view = this; | |
// receive updated information from anywhere | |
// and set on the model | |
$('body').on('updateHeader', function(e, data) { | |
view.model.set(data); | |
}); | |
// rivet things together | |
rivets.bind(view.$el, {status: view.model}); | |
} | |
}); |
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 View | |
// | |
var AppView = Backbone.View.extend({ | |
events: { | |
'click #uptodate' : function() { | |
this.$el.trigger('updateHeader', { hasUpdate: false } ); | |
}, | |
'click #outofdate' : function() { | |
this.$el.trigger('updateHeader', { hasUpdate: true } ); | |
} | |
}, | |
... |
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
<body> | |
<header> | |
<h2>Status: </h2> | |
<ul> | |
<li data-rv-hide="status.hasUpdate" rel='status'>Up to Date</li> | |
<li data-rv-show="status.hasUpdate" rel='status'>I'm Out of Date</li> | |
</ul> | |
</header> | |
<button id='uptodate'>Set Up to Date</button> | |
<button id='outofdate'>Set Out of Date</button> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment