timpone
timpone

Reputation: 19939

confused about using instances of Ember objects and whether items should be declared as properties or what that even means

I would like to get my feet wet in using Ember by using Ember Objects for my models and am using this part of the guide: http://emberjs.com/guides/object-model/classes-and-instances/#toc_initializing-instances . I have a class named InventoryItem and am not using EmberData nor EmberObject.

window.App = Ember.Application.create();
App.InventoryItem = Ember.Object.extend({
    init:function(){
      var idTEST=this.get('id');  // <- id in class but idTEST so that clear
      var binNumber=this.get('bin_number');
    },
    say:function(){
      alert('here is my binNumber:' + binNumber); // <- undefined
      alert('here is my binNumber:' + this.binNumber); // <- undefined
      alert('here is my binNumber:' + this.get('bin_number')); // <- this works
    }
 
});

var ii=App.InventoryItem.create({ id:12, bin_number:'abc123'})

What would idTEST represent and binNumber represent? Are there properties in Ember? When I use them later, I can't use ii.idTEST or ii.binNumber and use ii.get('id') and ii.get('bin_number'). What would be the point of setting these 'properties' up (if that's what they are)?

Later in my app, I am using an instance of an Ember class intrpolated with javascript - should I be using like ii.get('bin_number')?

thx

edit 1

do I need to do null checks for every item? can I mixin something to set to ''?

I tried

App.InventoryItem = Ember.Object.extend({
    inventory_notes:'',  // <- i thought that this would set it to empty string as a default if null but doesn't seem to work that way

  ....

  if(this.get('inventory_notes')===null){
    alert('you are null');
    this.set('inventory_notes','');
  }

Upvotes: 0

Views: 48

Answers (1)

Davide
Davide

Reputation: 69

idTEST and binNumber variables are defined in the init function and they will be visible within the function scope only. In the say function those variables will not be defined instead, so that if you call ii.say() you will cause an exception. This plain Javascript.

What you want to do is using the object properties you are passing to the create function and using get and set functions to read and modify the value of such properties:

window.App = Ember.Application.create();
App.InventoryItem = Ember.Object.extend({
   init: function() {
       // here you can do some initializations, e.g.
       this.set('test', this.get('id') + 1);
   },

   say:function(){
      alert('here is my binNumber:' + this.get('id'));
      alert('here is my binNumber:' + this.get('test')); // this will be 13
      alert('here is my binNumber:' + this.get('bin_number'));
   }
});

var ii=App.InventoryItem.create({ id:12, bin_number:'abc123'})

You can read more at Ember guide

Hope it helps!

Upvotes: 2

Related Questions