Fran b
Fran b

Reputation: 3036

Initialize model with a default attribute that assigns from other attribute

The Time model has two defaults attributes duration and remainingTime and I need that remainigTime values will depend to the duration.

class Time extends Backbone.Model
    defaults:
      duration: 0
      remainingTime: @duration  //This don't work

How can I access duration value for assign it to remainingTime?

PD: In addition I tested with Time.duration, Time.defaults.duration, this.duration

Upvotes: 0

Views: 48

Answers (2)

Ulug'bek
Ulug'bek

Reputation: 2832

I use a way like this: create a js(or coffee) which keeping global variables. so, it is following that:

root = @
#and
$ ->
  root.duration # or your other global variables

after than you can call the variable everywhere.

Upvotes: 0

Loamhoof
Loamhoof

Reputation: 8293

You can't access it like that because when the defaults object will be evaluated, your context won't be your object (rather the global object). Therefore remainingTime will certainly be undefined.
You could do it in the initialize method though.

Upvotes: 2

Related Questions