Backbone trigger don't fire

21 views
Skip to first unread message

David Hernández

unread,
Apr 20, 2020, 2:02:08 PM4/20/20
to backbonejs
Hello, i'm working in an extension  for the Point of Sale Module for ODoo https://www.odoo.com/.  The POS part of this ERP is based on backbone.js


I need to lauch a trigger when I click a button, and no when a model or collection change. By that i've added an  attribute tho this model as  that change only when I click the button:


var OrderSuper = models.Order.prototype;
models.Order = models.Order.extend({
initialize: function(attributes, options){
var self = this;
this.set('send_to_kitchen' , '0')
OrderSuper.initialize.apply(this, arguments);

},
saveChanges: function(){
var self = this;
this.set('send_to_kitchen' , '1') // PUT ATTRUBUTE TO 1
var lines_to_push =[];
var orderlines = this.orderlines.models;
for (var i=0; i < orderlines.length; i ++) {
if (orderlines[i].state && orderlines[i].state == 'Need to confirm') {
orderlines[i].state = 'Confirmed';
}
}
OrderSuper.saveChanges.apply(this, arguments);
this.set('send_to_kitchen' , '0',{silent: true}) // RESET TO 0
}
});



Later, another link in another model detects this attribute change and this activates the trigger to which the kitchen view is linked

models.PosModel = models.PosModel.extend({
initialize: function(session,attributes){
var self = this;
PosModelSuper.prototype.initialize.call(this,session,attributes);

this.get('orders').bind('change:send_to_kitchen', function(){
console.log('ENTER) //THIS IS WRITED TO THE CONSOLE,

this.trigger('sended_to_kitchen')

},this)
},

........

This is part of the code in the kitchen view

this.pos.bind('sended_to_kitchen', function(){
console.log('ORDERED')
},this);


The kitchen view link is never reached if I start the trigger function inside bind ('change: send_to_kitchen'), but if bind ('change: send_to_kitchen', I change it to bind ('change'), the kitchen receives the originated trigger


I hope I explain well the problem,,  What I'm doing wrong?
Reply all
Reply to author
Forward
0 new messages