你可以扩展初始化模式? [英] Can you extend initialized models?

查看:177
本文介绍了你可以扩展初始化模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类TheModel扩展Backbone.Model
    富:
        #酒吧

实体=新TheModel(#pass的属性)

我可以再扩展实体和维护模型属性/状态?

 类用户扩展实体
    富:
        超
        #更多

编辑:

 实体类扩展Backbone.Model
    初始化:(选项) - GT;
        @_events选项        _events:(选项) - GT;
            entity.bind'富'类ENTITY1扩展实体
    _events:(选项) - GT;
        超级选项
        entity.bind'酒吧'类ENTITY2扩展实体
    _events:(选项) - GT;
        超级选项
        entity.bind巴兹#A新的实体到达,我们不知道他是否是键入一个或两个又那么他是...
实体=新实体#now我们发现他是2型
实体=新ENTITY2(entity.attributes)


解决方案

没有。有一个类扩展一个对象(而不是函数)是没有意义的。如果你试图实例化用户类,你会得到错误

 类型错误:无法读取的未定义的属性构造

如果你想凌驾于实体方法,你应该这样做直接,但要注意的超级语法将不起作用。相反,你应该写

  entity.foo =  - >
  @constructor :: foo.apply参数#相当于超
  #...

class TheModel extends Backbone.Model
    foo: 
        #bar

Entity = new TheModel(#pass in attributes)

Can I then extend Entity and maintain the models attributes/state?

class User extends Entity
    foo: 
        super
        #more

EDIT:

class Entity extends Backbone.Model
    initialize: (options) ->
        @_events options

        _events: (options) -> 
            entity.bind 'foo'

class Entity1 extends Entity
    _events: (options) ->  
        super options
        entity.bind 'bar'

class Entity2 extends Entity
    _events: (options) -> 
        super options
        entity.bind 'baz'

#a new entity arrives, we don't know if he is type one or two yet so he is...
entity = new Entity

#now we find out he is type 2
entity = new Entity2(entity.attributes)

解决方案

No. Having a class extend an object (rather than a function) doesn't make sense. If you tried to instantiate your User class, you'd get the error

TypeError: Cannot read property 'constructor' of undefined

If you want to override the foo method on Entity, you should do so directly, though note that the super syntax won't work. Instead, you should write

entity.foo = ->
  @constructor::foo.apply arguments  # equivalent to super
  # ...

这篇关于你可以扩展初始化模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆