-
-
Save mxriverlynn/2489539 to your computer and use it in GitHub Desktop.
view helpers for underscore
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 data = { | |
type: "something", | |
name: "whatever", | |
addresses: [ | |
{ | |
type: "read_address", | |
address: "1/2/3", | |
value: "10" | |
}, | |
{ | |
type: "write_address", | |
address: "1/2/4" | |
} | |
] | |
} |
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
<script id="my-template" type="text/template"> | |
<p>type: <%= type %></p> | |
<p>name: <%= name %></p> | |
<p>read address: <%= _.find(this.addresses, function(addr){ return addr.type == "read_address" }).address %> </p> | |
<p>read address value: <%= _.find(this.addresses, function(addr){ return addr.type == "read_address" }).value %> </p> | |
<p>write address: <%= _.find(this.addresses, function(addr){ return addr.type == "write_address" }).address %> </p> | |
</script> |
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 viewHelpers = { | |
getAddress: function(type){ | |
var address = _.find(this.addresses, function(addr) { | |
return addr.type == type | |
}); | |
return address; | |
} | |
} | |
_.extend(data, viewHelpers); | |
_.template(myTemplate, data); |
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
<script id="my-template" type="text/template"> | |
<p>type: <%= type %></p> | |
<p>name: <%= name %></p> | |
<p>read address: <%= getAddress("read_address").address %> </p> | |
<p>read address value: <%= getAddress("read_address").value %> </p> | |
<p>write address: <%= getAddress("write_address").address %> </p> | |
</script> |
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.View.extend({ | |
template: "#my-template", | |
render: function(){ | |
var data = this.model.toJSON(); | |
_.extend(data, viewHelpers); | |
var html = _.template($(this.template), data); | |
this.$el.html(html); | |
} | |
}); |
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.Marionette.ItemView.extend({ | |
template: "#my-template", | |
templateHelpers: viewHelpers | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment