javascript - Chaplin/Backbone issue - "Add" event not fired when adding item to the collection -
i'm building test application myself, learn more coffeescript, backbone, brunch.io , chaplin js, i'm stuck , can't figure out i'm doing wrong.
this code in todo-view.coffee:
view = require 'views/base/view' todoitemview = require 'views/todo/todo-item' todoitemmodel = require 'models/todo/todo-item-model' todoitemcollection = require 'models/todo/todo-collection' # site view top-level view bound body. module.exports = class todoview extends view # template data stuff container: 'todo-container' tagname: 'ul' classname: 'todo-list' template: require './templates/todo' # create custom initialize method initialize: -> super # create new backbone collection dummy data store @collection = new todoitemcollection() # store dummy data in collection data = ["working", "studying", "gym", "sleep"] todoitem in data @collection.add( new todoitemmodel({ item: todoitem }) ) # render view @render() # listen data events listen: "add collection": "rendertodolist" # when template initialized need load # list items , append them container rendertodolist: -> # loop on items model in @collection.models todoitemview = new todoitemview({ container: @$el, model: model })
the problem is: event listener ( set in listener object ) isn't triggered. @rendertodolist isn't called.
calling @rendertodolist directly @initialize function work though. want function triggered "add" event on collection.
i've tried trigger event manually adding @collection.trigger "add" in loop creates new data models. didn't work neither.
any ideas i'm overseeing or doing wrong?
stefan,
i had similar issues when tried use listen hash events. opted setup listener such in initialize method of view.
@listento @collection, 'add', @rendertodolist, @
-hans
Comments
Post a Comment