Can you please suggest me some example for showing list view through marionette template system. Basically, I have a marionette template and based on the template i am creating a table list.
相关问题
- Backbone.js PushState routes .htaccess only workin
- Updating a LayoutView's Model
- Disable Backbone.js hashes entirely, but keep push
- Is there an easy way to distribute a Flask server
- React + Backbone, Target container is not a DOM el
相关文章
- Get all models in backbone collection where attrib
- How can I dynamically set a className for a Backbo
- Nesting Views within Views in backbone js
- Backbone-relational hasmany best practices
- JavaScript error: “is not a constructor”
- Marionette + i18n in templates
- Rendering reCAPTCHA v2.0 widget within Backbone vi
- Backbone.js PUT/DELETE problems with Codeigniter R
To create a list of table rows with Backbone Marionette you'll need to define five things:
CollectionView
to iterate through the collectionItemView
to provide row-specific functionalityItemView
that provides the markup for each rowSample Use Case
Say you have the following data:
Define a model and collection
For the following data you'd want to have a model:
where you can set up some sensible defaults for your data and other properties, like
idAttribute
. There are plenty of references on how to customize your model. Your collection should, at minimum, take thestoogeModel
.Set up your views
To iterate and render your collection into a table, all you'll need is a
CollectionView
and anItemView
.All
CollectionViews
need at minimum achildView
which is anItemView
(which we define below) that they will use to derive the functionality to create the html for each row, and acollection
which is the collection holding the models that populate each row. We'll pass this collection in when we instatiate astoogesCollectionView
. ThetagName
property tells Backbone to encapsulate the children in atable
tag.All
ItemViews
require a template which we define in our HTML document, here it's#stooge-template
. If theItemView
is a child of a collection you don't have to define its model, it will be provided by the parentCollectionView
. The HTML rendered by each childStoogeItemView
will be encapsulated bytr
tags, because what we want is a row for each child of the collection view.From the
ItemView
you can handle events at the "row" level, likeclick
orfocus
on a row column. You can also handle changes to the model that was passed in. Additionally, from theItemView
you could decide to pass in helpers that can manipulate the way data is displayed in its template.Putting it all together
Now we have defined our model, collection, collection view, child item view, and the item view template. Next, we'll connect all these pieces.
Populate your collection
You pass in the array of stooges to the constructor of your collection.
These are shaped into models, and they load your collection.
Rev up your
CollectionView
You pass your loaded collection to your collection view.
Render your view
All Marionette views come packaged with a
render
method. In our case, invokingwill create a
<table>
with three<tr>
elements each with a column each for thename
,age
, anduserid
fields in our dataset. To insert the resulting HTML in the DOM we simply use the view'sel
property. For simplicty, we can inject the view straight into the bodyor using jQuery:
Beyond this example
There is much functionality that we didn't even begin to discuss. Read the docs! And check out the many many tutorials. Hope this brief intro helps!