[英]converting coffee script to javascript - simple extend
我有一些咖啡劇本-
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
});
};
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不會執行任何此類操作,並且模型和視圖無論如何都是非常不同的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.