Created
July 29, 2011 17:05
-
-
Save mxriverlynn/1114226 to your computer and use it in GitHub Desktop.
knockout-style data-bind attributes for backbone.js
This file contains 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 DataBindConvention = { | |
selector: "*[data-bind]", | |
handler: { | |
bind: function(selector, view, model){ | |
view.$(selector).each(function(index){ | |
var element = view.$(this); | |
var databind = element.attr("data-bind").split(" "); | |
var elementAttr = databind[0]; | |
var modelAttr = databind[1]; | |
model.bind("change:" + modelAttr, function(changedModel, val){ | |
switch(elementAttr){ | |
case "html": | |
element.html(val); | |
break; | |
case "text": | |
element.text(val); | |
break; | |
default: | |
element.attr(elementAttr, val); | |
} | |
}); | |
}); | |
} | |
} | |
}; | |
Backbone.ModelBinding.Conventions.dataBind = DataBindConvention; |
This file contains 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 vent = _.extend({}, Backbone.Events); | |
AddEditView = Backbone.View.extend({ | |
events: { | |
"click #save": "save" | |
} | |
initialize: function(options){ | |
this.vent = options.vent; | |
}, | |
save: function(){ | |
this.model.save(); | |
this.vent.trigger("modelUpdated"); | |
} | |
}); | |
MedicationView = Backbone.View.extend({ | |
initialize: function(options){ | |
_.bindAll(this, "render"); | |
options.vent.bind("modelUpdated", this.render); | |
}, | |
render: function(){ | |
// ... jQuery template stuff here | |
} | |
}); |
This file contains 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
AddEditView = Backbone.View.extend({ | |
render: function(){ | |
// ... jQuery template stuff here | |
Backbone.ModelBinding.call(this); | |
} | |
}); | |
MedicationView = Backbone.View.extend({ | |
render: function(){ | |
// ... jQuery template stuff here | |
Backbone.ModelBinding.call(this); | |
} | |
}); |
This file contains 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
<fieldset> | |
<legend data-bind="text trade_name">Some drugs</legend> | |
<ul> | |
<li class="medication-data"> | |
<div class="label">Dosage</div> | |
<div class="data" data-bind="text dosage">5000mg</div> | |
</li> | |
<li class="medication-data"> | |
<div class="label">Route</div> | |
<div class="data" data-bind="text route">something</div> | |
</li> | |
... | |
</ul> | |
</fieldset> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment