Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:54:51+00:00 2026-06-04T19:54:51+00:00

I’m wondering how I should define instance variables inside a Backbone Model. This is

  • 0

I’m wondering how I should define instance variables inside a Backbone Model. 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). 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:54:52+00:00Added an answer on June 4, 2026 at 7:54 pm

    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. So I do some metaprogramming and generate named get/set methods. 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.

    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"). 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")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.