backbone - event not fired on selected element changed

656 views
Skip to first unread message

lszk

unread,
Aug 8, 2016, 6:14:50 AM8/8/16
to backbonejs

I'm generating a drop down list from Backbone.View.
After attaching it to the DOM, change event is not fired. The delegateEvents doesn't fixes it. Can somebody show me where the blind spot is?


Model and collection:


App.Models.DictionaryItem = Backbone.Model.extend({
        default: {
            key: '',
            value: '', id: 0
        }
    });

    App.Collections.Dictionary = Backbone.Collection.extend({
        model: App.Models.DictionaryItem,
        initialize: function (models, options) {

        },
        parse: function (data) {
            _.each(data, function (item) {
             //   if(item){
                var m = new App.Models.DictionaryItem({ key: item.code, value: item.name });
                this.add(m);
           // }
            }, this);
        }
    });

Views:

App.Views.ItemView = Backbone.View.extend({ tagName: 'option', attributes: function () { return { value: this.model.get('key') } }, initialize: function () { // this.template = _.template(this.model.get('value')); }, render: function () { // this.$el.html(this.template());

this.$el.empty().html(this.model.get('value'))  return this; } }); App.Views.CollectionView = Backbone.View.extend({ tagName: 'select', attributes: { 'class': 'rangesList' }, events: { 'change .rangesList': 'onRangeChanged' }, initialize: function (coll) { this.collection = coll; }, render: function () { _.each(this.collection.models, function (item) { this.$el.append(new App.Views.ItemView({ model: item }).render().el); }, this); // this.delegateEvents(this.events); return this; }, selected: function () { return this.$el.val(); }, onRangeChanged: function () { alert('changed'); } });

Rendering:

var coll = new App.Collections.Dictionary(someData, { parse: true }); var v= new App.Views.CollectionView(coll); var vv=v.render().el; // new App.Views.CollectionView(coll).render().el; $('body').append(vv)

Jason Crawford

unread,
Aug 8, 2016, 11:08:52 AM8/8/16
to backb...@googlegroups.com
I don't think 'change' events are fired when an element is attached to the DOM. The 'change' event is for form inputs, I think—when the input changes.

Hope that helps,
Jason


--
The Unofficial Backbone.js Group
 
Job Board: https://groups.google.com/forum/#!topic/backbonejs/wHRdZczyOEc
---
You received this message because you are subscribed to the Google Groups "backbonejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to backbonejs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Fieldbook: Create a database, as easily as a spreadsheet
Reply all
Reply to author
Forward
0 new messages