In my project I am using Marionette with stickit for two way data binding.
In there I need to clone div element which has select and textbox.
var $template = $('#template-clone');
var $clone = $template.clone();
$clone.show();
var observeSelectID = $clone.find('select').attr('id');
var selectID = '#'+observeSelectID;
after that I am trying to bind newly created element using stickit.
this.addBinding(null, {
selectID : {
observe : observeSelectID,
initialize : function($el) {
$(selectID).select2({
});
},
},
selectOptions : {
collection : function(data) {
var option = "{Label value pair JSON String}"
return {
'opt_labels' : ['List'],
'List' : option
};
}
}
});
But any changes in the select box won't trigger model changes.
Got the solution: Add the handler for select2 stickit.
Backbone.Stickit.addHandler({
selector : 'select.select2',
initialize : function($el, model, opt) {
var e = 'change:' + opt.observe,
self = this;
$el.select2();
var up = function(m, v, o) {
if (!o.stickitChange)
$el.trigger("change");
};
this.listenTo(model, e, up);
this.listenTo(model, 'stickit:unstuck', function() {
self.stopListening(model, e, up);
});
}
});
and when new element is created add its bindings
var textareaObserve = $('#textarea').attr('id');
var textareaID = '#' + textareaObserve;
self.bindings[selectedID] = observeSelectID;
self.addBinding(self.model, selectedID, observeSelectID);
will make select2 bindings.