我想知道如何在主干模型中定义实例变量。这是我目前正在这样做的方式:
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
这似乎有点多余,@set columnGroups: @getColumnGroups()
并且必须使用每个变量@get("...")
似乎有点冗长(我希望我能这样做@variableName
)。我的问题是,这是使用模型和实例变量的正确方法还是我做错了?另外,这样做有什么区别吗?:
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()
然后,从刚刚做的观点来看@model.columnNames