JJJ
JJJ

Reputation: 2899

Ember static variables

I'm trying to have several views access the same variable. I guess the easy solution would be to have a controller store the variable for views. But is there anyway to declare static variables in ember?

Upvotes: 1

Views: 1672

Answers (3)

Boban Mijajlovic
Boban Mijajlovic

Reputation: 141

There are two ways to define variable to behave like static variable in Ember object. One way is in reopenClass function and another way is to define variable when defining the class in extend function of Ember.Object. The example bellow show these two ways.

App.Person = Ember.Object.extend({
    data2: "Boban", // static variable
    setData: function(value){
           App.Person.data=value;
    },
    getData: function(){ return App.Person.data;},
    setData2: function(value){
        App.Person.prototype.data2=value;
    },
    getData2: function(){return  App.Person.prototype.data2;}
 }).reopenClass({
                 data: 123 // static variable
               });


 per1= App.Person.create();
 per2=App.Person.create();
 console.log("show 1 th...");
 console.log("data per1 : " + per1.getData());   //data per1 : 123
 console.log("data per2 : " + per2.getData());   //data per2 : 123
 console.log();
 per1.setData("data is set in per 1");
 console.log("show 2 th...");
 console.log("data per1 : " + per1.getData()); //data per1 : data is set in per 1
 console.log("data per2 : " + per2.getData());  //data per2 : data is set in per 1
 console.log();
 per2.setData("data is set in per2");
 console.log("show 3 th...");
 console.log("data per1 : " + per1.getData());  //data per1 : data is set in per2
 console.log("data per2 : " + per2.getData());   //data per2 : data is set in per2
 console.log();


console.log("show 11 th...");
console.log("data2 per1 : " + per1.getData2()); //Boban
console.log("data2 per2 : " + per2.getData2()); //Boban
console.log();
per1.setData2("data2 is set in per 1");
console.log("show 12 th...");
console.log("data2 per1 : " + per1.getData2()); //data2 per1 : data2 is set in per 1
console.log("data2 per2 : " + per2.getData2());  //data2 per2 : data2 is set in per 1
console.log();
per2.setData2("data2 is set in per2");
console.log("show 13 th...");
console.log("data2 per1 : " + per1.getData2()); //data2 per1 : data2 is set in per2
console.log("data2 per2 : " + per2.getData2());//data2 per1 : data2 is set in per2
console.log();

// end at last
per1.setData("D1");
per2.setData2("D2");

per1.set("data","df1");
per2.set("data","df2");

per1.set("data2","d2f1");
per2.set("data2","d2f2");

console.log("last ....")
console.log("per1 : " + per1.getData()  + "     " +  per1.get("data")  + "          " +  per1.getData2() + "     " + per1.get("data2"));
console.log("per2 : " + per2.getData()  + "     " +  per2.get("data")  + "         " +  per2.getData2() + "     " + per2.get("data2"));


 //per1 : D1     df1     D2     d2f1
 //per2 : D1     df2     D2     d2f2

As it can be seen that variables data and data2 behave like static variable. if You try to set the variable with same name with function set .set('data', 'df') that is new variable for that instance and are not the same like static one with the same definition.

Upvotes: 1

Xiujun Ma
Xiujun Ma

Reputation: 2584

You also can define an view and set the static variables in it, and other views extend this view.

Upvotes: 0

JJJ
JJJ

Reputation: 2899

Solution is to reopen class and add in the static variable like so. What is the ember.js idiomatic way to create static variables in model?

Upvotes: 1

Related Questions