English 中文(简体)
在后骨模型中定义实例变量的适当方式是什么?
原标题:What is the proper way to define instance variables in Backbone models?

我在想我该如何定义一个后骨模型中的实例变量。这是我目前这样做的方式:

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

这似乎有点多余, 需要做 , 并且需要使用 来获取每个变量 。 似乎有点动词( 我希望我可以做 ) 。 我的问题是, 这是使用模型和实例变量的正确方法, 还是我做错了? 另外, 这样做有什么区别吗?

    class GeneSet extends Backbone.Model
        initialize: (parsedGenes)->
            @parsedGenes = parsedGenes
            @geneNames = (gene.gene_name for gene in @parsedGenes)
            @geneIds = ("gene_#{id}" for id in [[email protected]])
            @clusters = (gene.cluster for gene in @parsedGenes)
            @descriptions = (gene.description for gene in @parsedGenes)
            @columnNames = @getColumnNames()
            @columnGroups = @getColumnGroups()
            @geneExpressions = @getGeneExpressions()
            @groupedGeneExpressions = @getGroupedGeneExpressions()
            @extent = @getExtent()

然后,从视觉上看, 只是在做

最佳回答

这里是我在基本模型超级类中所做的。 这几乎与主干设计相悖, 但我非常讨厌使用 get set set , 我想要所有属性访问背后的真正方法。 所以我做一些元程序设计, 并生成名为获取/ 设置的方法 。 因此, 我不需要这样做 :

model.get("name")
model.set("name", "Tom")

我只要做就行了

model.name()
model.name("Tom")

s 在这里, 我的基代码可以自动对任意 < code> 属性 对象自动进行此操作 。

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

注意, 这是写在不支持 set (“ keys, “ value” ) 的骨干版本上。 如果我要更新它, 我可能会使用该变量。 注意, 由于 set set 口味返回了对象, 注意它们是链式的 : model. name (“ John” ). email (“ a href=" / cdn- cgi/l/ email- protection" 类=" __cf_ email_ ” Data- cfemail="f9969197b99c.819894899c79c79a9694" > [emailproted] .

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.