最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

odoo - Javascript inheritance on variable within a function (OpenERP) - Stack Overflow

programmeradmin1浏览0评论

Basically I'm trying to override a function by extending it. I have the following base (simplified) code:

openerp.point_of_sale = function(db) {

    var Order = Backbone.Model.extend({

        exportAsJSON: function() {
            return {'bigobject'}
        }
    })
}

Then, I'm writing my own .js where I want to inherit and override exportAsJSON function and I'm not sure how to .extend it. Here is my erroneous approach:

openerp.my_module = function(db) {

    db.point_of_sale.Order = db.point_of_sale.Order.extend({

        exportAsJSON: function() {

            var order_data = this._super();
            //... add more stuff on object
            return order_data;
        }
    })
}

What would be the correct way of doing it?

I hope I'm providing enough information for an answer (I'm working on OpenERP by the way). Any help will be appreciated.

EDIT: More specifically, the error seems to be in the extension itself:

db.point_of_sale.Order = db.point_of_sale.Order.extend({

...even if I put a simple return 0; within my exportAsJSON function, the page doesn't load and I get the following error in my browser console:

"Cannot call method 'extend' of undefined" 

Basically I'm trying to override a function by extending it. I have the following base (simplified) code:

openerp.point_of_sale = function(db) {

    var Order = Backbone.Model.extend({

        exportAsJSON: function() {
            return {'bigobject'}
        }
    })
}

Then, I'm writing my own .js where I want to inherit and override exportAsJSON function and I'm not sure how to .extend it. Here is my erroneous approach:

openerp.my_module = function(db) {

    db.point_of_sale.Order = db.point_of_sale.Order.extend({

        exportAsJSON: function() {

            var order_data = this._super();
            //... add more stuff on object
            return order_data;
        }
    })
}

What would be the correct way of doing it?

I hope I'm providing enough information for an answer (I'm working on OpenERP by the way). Any help will be appreciated.

EDIT: More specifically, the error seems to be in the extension itself:

db.point_of_sale.Order = db.point_of_sale.Order.extend({

...even if I put a simple return 0; within my exportAsJSON function, the page doesn't load and I get the following error in my browser console:

"Cannot call method 'extend' of undefined" 
Share edited Oct 31, 2012 at 13:14 nicobustillos asked Oct 30, 2012 at 18:54 nicobustillosnicobustillos 1531 silver badge10 bronze badges 1
  • We need to see more of your code. Maybe a jsFiddle? – Trevor Dixon Commented Oct 30, 2012 at 23:36
Add a ment  | 

3 Answers 3

Reset to default 2

I think you want something like SuperClass.prototype.method.call(this):

openerp.my_module = function(db) {

    db.point_of_sale.Order = db.point_of_sale.Order.extend({

        exportAsJSON: function() {

            var order_data = db.point_of_sale.Order.prototype.exportAsJSON.call(this);
            //... add more stuff on object
            return order_data;
        }
    })
}

This is how you would normally do that in JavaScript:

var eaj = db.point_of_sale.Order.prototype.exportAsJSON;

db.point_of_sale.Order = db.point_of_sale.Order.extend({
    exportAsJSON: function() {

        var order_data = eaj.apply( this, arguments );
        //... add more stuff on object
        return order_data;
    }
})

This is basically where you problem lies:

openerp.point_of_sale = function(db) {
    var Order = Backbone.Model.extend({
     ^
     |
  this is a private variable
  not a property!

Therefore you cannot access it at all. If it was defined like this:

openerp.point_of_sale = function(db) {
    openerp.point_of_sale.Order = Backbone.Model.extend({
                           ^
                           |
                     this is now a property of point_of_sale
                     (basically public variable)

then you can access it the way you're trying to:

db.point_of_sale.Order = db.point_of_sale.Order.extend({

So, the answer is you cannot do that. You need to extend or modify db.point_of_sale instead of Order.

发布评论

评论列表(0)

  1. 暂无评论