Basic Sproutcore: class method, class variables help

This is how I define a simple class with instance variables and instance methods.

ExampleClass = SC.Object.extend({
    foo:undefined,
    bar: function() {
        this.foo = "Hello world";
        console.log( this.foo );
    }
}

// test
var testInstance = ExampleClass.create();
testInstance.bar();    // outputs 'Hello world'

Can someone help me with a similar example of a class variable (or similar behavior) and a class method?

thank

0
source share
1 answer

The class method / property will be executed as follows:

ExampleClass = SC.Object.extend({
  foo:undefined,
  bar: function() {
    this.foo = "Hello world";
    console.log( this.foo );
  }
}

ExampleClass.mixin({
  classFoo: "foo",
  classBar: function() {
    return "Bar";
  }
})

Then you can access it, for example:

ExampleClass.classFoo

But do not forget that when accessing a property (or a computed property) in an instance you need to use .get()as:

var example = ExampleClass.create();
// Good
example.get('foo');
example.set('foo', 'baz');

// BAD!! Don't do this, or Bindings/ Observes won't work.
example.foo; 
example.foo = 'baz';
0
source

All Articles