Last active
August 29, 2015 13:58
-
-
Save berzniz/10194472 to your computer and use it in GitHub Desktop.
Backbone tips & rules
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
// Ok: | |
this.stateModel.on('change:readMore', this.renderReadMore, this); | |
// Awesome: | |
this.listenTo(this.stateModel, 'change:readMore', this.renderReadMore); |
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
view.render().$el.appendTo(otherElement); |
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
this.listenTo(this.stateModel, 'change', this.render); |
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
this.listenTo(this.stateModel, 'change:readMore', this.renderReadMore); |
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
this.viewState.set('readMore', 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
model.fetch({ | |
success: handleSuccess, | |
error: handleError | |
}); |
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
view.listenTo(model, 'sync', handleSuccess); | |
view.listenTo(model, 'error', handleError); | |
model.fetch(); |
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
$('.text').html('Thank you'); |
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
this.$('.text').html('Thank you'); | |
// psssst... This is the same as: | |
// this.$el.find('.text').html('Thank you'); | |
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
var BodyView = Backbone.View.extend({ | |
initialize: function() { | |
this.listenTo(Backbone, 'prevent-scroll', this.preventScroll); | |
}, | |
preventScroll: function(prevent) { | |
// .prevent-scroll has the following CSS rule: overflow: hidden; | |
this.$el.toggleClass('prevent-scroll', prevent); | |
} | |
}); | |
// And now from anywhere in the code: | |
Backbone.trigger('prevent-scroll', true); // prevent scrolling | |
Backbone.trigger('prevent-scroll', false); // allow scrolling |
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
this.viewState = new Backbone.Model(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment