Created
January 28, 2012 08:06
-
-
Save kaineer/1693380 to your computer and use it in GitHub Desktop.
Ура, заработало!
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
| Backbone.Bindings = { | |
| setModel: function(model, bindings) { | |
| var view = this; | |
| bindings || (bindings = (this.bindings || {})); | |
| if(this.model) { | |
| this.model.unbind(); | |
| } | |
| _.each(bindings, function(i, selector) { | |
| var property = bindings[selector]; | |
| model.bind("change:" + property, function() { | |
| view.$(selector).val(model.get(property)); | |
| }); | |
| view.$(selector).unbind("change"); | |
| view.$(selector).bind("change", function(event) { | |
| var $this = $(event.target); | |
| var hash = {}; | |
| hash[property] = $this.val(); | |
| model.set(hash); | |
| }); | |
| }); | |
| this.model = model; | |
| } | |
| }; | |
| var BindedView = Backbone.View.extend(Backbone.Bindings); | |
| var TheModel = Backbone.Model.extend({ | |
| }); | |
| var TheView = BindedView.extend({ | |
| el: "div#target", | |
| bindings: {"#name": "name", "#weight": "weight"}, | |
| render: function() { | |
| $(this.el).html("<input id='name' /><br/><input id='weight'/>"); | |
| } | |
| }); | |
| var view = new TheView(); | |
| var model = new TheModel(); | |
| view.render(); | |
| view.setModel(model); | |
| model.set({name: "Hello", weight: 100}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment