admin管理员组文章数量:1290958
I have static properties that I would like to access from instances of my Backbone.Model objects. I know I could hardcode the parent constructor to call the method, but this prevents me from having polymorphic static functions. For example, I would like to be able to override the foo
function in ExtendedInventory
if necessary, without having to change any other code.
var Inventory = Backbone.Model.extend({},
//STATIC
{
foo: function() {
alert('bar');
}
});
var i = new Inventory({});
i.constructor.foo(); //This works!
var ExtendedInventory = Inventory.extend({});
var ei = new ExtendedInventory({});
ei.constructor.foo(); //THIS DOES NOT WORK
//How do I generically access the `Inventory.foo()` function via the `ei` object. I would
I have static properties that I would like to access from instances of my Backbone.Model objects. I know I could hardcode the parent constructor to call the method, but this prevents me from having polymorphic static functions. For example, I would like to be able to override the foo
function in ExtendedInventory
if necessary, without having to change any other code.
var Inventory = Backbone.Model.extend({},
//STATIC
{
foo: function() {
alert('bar');
}
});
var i = new Inventory({});
i.constructor.foo(); //This works!
var ExtendedInventory = Inventory.extend({});
var ei = new ExtendedInventory({});
ei.constructor.foo(); //THIS DOES NOT WORK
//How do I generically access the `Inventory.foo()` function via the `ei` object. I would
Share
Improve this question
asked Jun 28, 2011 at 18:39
aw crudaw crud
8,89121 gold badges76 silver badges117 bronze badges
3
- So, this works but I do not understand it very well and it only works in Firefox: ei.constructor.prototype.__proto__.constructor.foo() – aw crud Commented Jun 28, 2011 at 18:49
- 1 I wrote a blog post with examples of using static members with backbone that might be helpful to anyone ing across this question. taurenmills.wordpress./2011/10/08/… – Tauren Commented Oct 11, 2011 at 5:03
- +1 for drawing attention to class properties – Chuck Commented Mar 25, 2013 at 23:21
2 Answers
Reset to default 5Hmm. Though the code above does work, I wouldn't leave it that way. If the function is logically accessible through an object of the class, then define an instance method in the base class that calls the class/"static" function. This makes the code cleaner and clearer, I think (plus, clients don't have to remember the somewhat arcane syntax):
var Inventory = Backbone.Model.extend({
foo: function() {
this.constructor.foo();
}
}, {
foo: function() {
alert('bar');
}
});
var i = new Inventory({});
i.foo(); //This works!
var ExtendedInventory = Inventory.extend({});
var ei = new ExtendedInventory({});
ei.foo();
What doesn't work exactly, in your example? In both Firefox and IE, I get two popups with 'bar', which looks like the intended result? The static part of it appears to behave fine as well, see this jsfiddle.
(this is with the HEAD version of Backbone, btw; don't know if that makes a difference)
本文标签:
版权声明:本文标题:javascript - How can I call a static Backbone.Model function from an instance of that model, without specifying the model name? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523256a2383314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论