Created
June 27, 2013 10:44
-
-
Save gillesruppert/5875554 to your computer and use it in GitHub Desktop.
Marionette.CollectionView for a select box
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
<select> // CollectionView.el | |
<option value="modelId">Model.name</option> // ItemView for the model | |
</select> | |
<!-- | |
can you have Marionette views that do not have a template without overriding the render method? Overriding the render method is painful as you have to implement the `isClosed setting plus all the event triggers, so it keeps working with all the Marionette hooks, or am I mistaken? | |
--> |
if you really want to use marionette for this, don't use a collectionview. the option
tag is very simple, and trying to get an ItemView to render each tag is a big waste, honestly. too much code for no benefit.
you could do it this way, instead:
<script id="select-template" type="text/html">
<% _.each(items, function(item){ %>
<option value="<%= id %>"><%= name %></option>
<% }) %>
</script>
var SelectView = Marionette.ItemView({
template: "#select-template",
tagName: "select"
});
var c = new MyCollection();
// load your data here
var sv = new SelectView({
collection: c
});
someRegion.show(sv);
Where does items
come from?
Hello derickbailey,
Your code is very much excellent, but has one small mistake , You miss item ..
html code looks like this .
Same solution, well explained. http://stackoverflow.com/a/18194457/374851
An ItemView can take a collection in to it, and you can access that collection at items inside of the template.
and it's written in document too.
https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.itemview.md#rendering-a-collection-in-an-itemview
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, I think I've worked around this:
instead of using a
Marionette.ItemView
, I'm using a very basicBackbone.View
that has a simple render method which just sets thevalue
andinnerText
of theview.el
. This delivers the result as expected.If however you think there is a better way, please let me know!