Hello
I'm exploring with CanJS controls, views and live binding.
I want to create a two-level control structure where I have one ItemsController
that holds multiple ItemController inside:
Here are my EJS templates:
itemsTemplate.ejs :
itemTemplate.ejs :
This is my current structure:
This seems to work well on init but when I later push new item to testItems, all 3 existing items completely dissappear from the view and ItemControl.destroy gets called 3 times.
Could someone give me an example how to do this two-level control structure so that it works well with live binding?
I'm exploring with CanJS controls, views and live binding.
I want to create a two-level control structure where I have one ItemsController
that holds multiple ItemController inside:
Here are my EJS templates:
itemsTemplate.ejs :
- <% items.each(function(item) { %>
- <div
id="itemHolder-<%=item.attr('id') %>"></div>
- <% }); %>
itemTemplate.ejs :
- <div class="item" <%= (el) -> el.data('item',
item) %>>
- <%=item.attr('name') %>
- </div>
This is my current structure:
- var itemsTemplate = require('text!views/itemsTemplate.ejs');
- can.view.ejs('itemsTemplate', exampleTemplate);
- var ItemsControl = can.Control({
-
- init: function(el, options) {
-
-
can.view('itemsTemplate', {items: options.items}, function(template){
- that.element.html(template);
-
-
// Create items into itemHolder "sandboxes"
-
$.each(options.items, function(key, item){
-
var itemControl = new ItemControl('#itemHolder-'+item.attr('id'),
{item: item});
- }
- });
- },
-
- destroy: function() {
- console.log('destroy
called for ItemsControl');
-
can.Control.prototype.destroy.call( this );
- }
- });
-
-
- var itemTemplate = require('text!views/itemTemplate.ejs');
- can.view.ejs('itemTemplate', itemTemplate);
-
- var ItemControl = can.Control({
-
- init: function(el, options) {
-
can.view('itemTemplate', {item: options.item}, function(template){
- that.element.html(template);
- });
- },
-
- destroy: function() {
- console.log('destroy
called for ItemControl');
-
can.Control.prototype.destroy.call( this );
- },
-
- 'click': function() {
- console.log('You
clicked item!');
- }
- });
-
- // Testing
- var testItems = new can.List();
- testItems.push({id: 1, name: 'Item #1'});
- testItems.push({id: 2, name: 'Item #2'});
- testItems.push({id: 3, name: 'Item #3'});
-
- // Render test items
- var testItemsControl = new ItemsControl('#items', {items: testItems});
-
This seems to work well on init but when I later push new item to testItems, all 3 existing items completely dissappear from the view and ItemControl.destroy gets called 3 times.
Could someone give me an example how to do this two-level control structure so that it works well with live binding?