【问题标题】:converting coffee script to javascript - simple extend将咖啡脚本转换为 javascript - 简单扩展
【发布时间】:2013-12-02 17:23:39
【问题描述】:

我有一些咖啡脚本 -

class Zoo.CollectionView extends Zoo.View

  _set_element_attributes: ->
    @$el.data(view: this)
    @$el.addClass('collection')
    return unless @collection?
    @$el.attr('data-name': @collection.collection_name)
    @$el.attr('data-variant': @variant) if @variant?
    @$el.data(collection: @collection, view: this)

我想转换为 javascript 并尽可能保持简单。我认为这会起作用,但它似乎不喜欢我的扩展。

 Zoo.CollectionView = function() {

 }

 $.extend(Zoo.CollectionView.prototype, Zoo.View.prototype);

 Zoo.CollectionView.prototype._set_element_attributes = function() {
   this.$el.data({
     view: this
   });
   this.$el.addClass('collection');
   if (this.collection == null) {
     return;
   }
   this.$el.attr({
     'data-name': this.collection.collection_name
   });
   if (this.variant != null) {
     this.$el.attr({
       'data-variant': this.variant
     });
   }
   return this.$el.data({
     collection: this.collection,
     view: this
   });
 };

【问题讨论】:

    标签: javascript inheritance coffeescript


    【解决方案1】:

    CoffeeScript 的 extends 与 Backbone 的 extend 兼容,因此您应该可以这样做:

    Zoo.CollectionView = Zoo.View.extend({
        _set_element_attributes: function() {
            // This part should be easy and it looks like you
            // have it in hand.
        }
    });
    

    我不明白您为什么要尝试将 Backbone.Model 的原型合并到 Zoo.CollectionView 的原型中:

    $.extend(Zoo.CollectionView.prototype, Backbone.Model.prototype);
    

    因为您的 CoffeeScript 并没有做类似的事情,而且模型和视图无论如何都是非常不同的东西。

    【讨论】:

    • 效果很好,而且非常简洁。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2013-11-21
    • 2013-10-30
    • 1970-01-01
    • 2012-02-22
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多