[英]What is the proper way to define instance variables in Backbone models?
I'm wondering how I should define instance variables inside a Backbone Model. 我想知道如何在Backbone模型中定义实例变量。 This is the way I'm currently doing it: 这就是我目前的做法:
class GeneSet extends Backbone.Model
initialize: (parsedGenes)->
@set parsedGenes: parsedGenes
@set geneNames: (gene.gene_name for gene in @get("parsedGenes"))
@set geneIds: ("gene_#{id}" for id in [1..@get("parsedGenes").length])
@set columnNames: @getColumnNames()
@set columnGroups: @getColumnGroups()
@set geneExpressions: @getGeneExpressions()
@set groupedGeneExpressions: @getGroupedGeneExpressions()
@set extent: @getExtent()
clusterColor: ->
d3.scale.category20()()
getGeneNameById: (geneId)->
@get("geneNames")[@get("geneIds").indexOf(geneId)]
getColumnGroups: ->
_.uniq((@get("columnNames")).map((columnName)->
columnName.split("_")[0]
))
getExtent: ->
expressions = _.flatten(@get("geneExpressions").map (geneExpression)->
geneExpression.map (item)->
item.y
)
d3.extent(expressions)
getColumnNames: ->
Object.keys(@get("parsedGenes")[0]).filter (columnName) ->
!columnName.match(/cluster/) && isNumber(parsedGenes[1][columnName])
getGeneExpressions: ->
@get("parsedGenes").map (gene) =>
@get("columnNames").map (columnName) ->
x: columnName
y: +gene[columnName] # make numeric
This seems a little redundant to do @set columnGroups: @getColumnGroups()
and having to get every variable using @get("...")
seems kind of verbose (I wish I could do @variableName
). 对于@set columnGroups: @getColumnGroups()
这似乎有点多余@set columnGroups: @getColumnGroups()
并且必须使用@get("...")
获取每个变量似乎有点冗长(我希望我能做@variableName
)。 My question is, is this the right way of using models and instance variables or am I doing it wrong? 我的问题是,这是使用模型和实例变量的正确方法还是我做错了? Also, is there any difference to doing this?: 这样做有什么不同吗?:
class GeneSet extends Backbone.Model
initialize: (parsedGenes)->
@parsedGenes = parsedGenes
@geneNames = (gene.gene_name for gene in @parsedGenes)
@geneIds = ("gene_#{id}" for id in [1..@parsedGenes.length])
@clusters = (gene.cluster for gene in @parsedGenes)
@descriptions = (gene.description for gene in @parsedGenes)
@columnNames = @getColumnNames()
@columnGroups = @getColumnGroups()
@geneExpressions = @getGeneExpressions()
@groupedGeneExpressions = @getGroupedGeneExpressions()
@extent = @getExtent()
And then, from the view just doing @model.columnNames
然后,从视图中做@model.columnNames
Here's what I do in my base model superclass. 这是我在基础模型超类中所做的。 It's pretty much against the backbone design, but I too hate using get
and set
and I want real methods behind all attribute access. 它几乎与主干设计相悖,但我也讨厌使用get
和set
,我想要所有属性访问背后的真实方法。 So I do some metaprogramming and generate named get/set methods. 所以我做了一些元编程并生成了命名的get / set方法。 So instead of having to do: 所以不必做:
model.get("name")
model.set("name", "Tom")
I can just do 我可以做
model.name()
model.name("Tom")
Here's my base code to do this automatically for any arbitrary attributes
object. 这是我的基本代码,可以自动为任意attributes
对象执行此操作。
addConvenienceMethods = ->
for prop, value of this.attributes
((prop) ->
#Define a setter/getter function
this[prop] = (newValue...) ->
if newValue.length
obj = {}
obj[prop] = newValue[0]
this.set obj
return this
else
return this.get prop
#Use the newly-defined setter function to store the default value
this[prop](value)
).call(this, prop)
########## Base Model Superclass ##########
class exports.Model extends Backbone.Model
initialize: ->
addConvenienceMethods.call this
Note this was written on a backbone version old enough to not support set("key", "value")
. 请注意,这是在一个足以支持set("key", "value")
的骨干版本上编写的。 If I were to update it, I'd probably use that variant. 如果我要更新它,我可能会使用该变体。 Note since the set
flavor returns the object, they are chainable: model.name("John").email("john@example.com").role("admin")
注意,因为set
flavor返回对象,它们是可链接的: model.name("John").email("john@example.com").role("admin")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.