Search code examples
ember.jshandlebars.jsember-model

How to use belongsTo in ember-model?


I have created FIXTURES using ember-model, but i am not able to use following node on template

    "logged_in": {
        "id" : "1",
        "logged": true,
        "username": "sac1245",
        "account_id": "4214"
    } 

I have implemented belongsTo relation for this node but it throwing one error : Uncaught Error: Ember.Adapter must implement findQuery
Here i have listed my model code :

Astcart.Application =  Ember.Model.extend({
    name: Ember.attr(),
    logo_url : Ember.attr(),
    list: Ember.hasMany('Astcart.List', {key: 'list', embedded: true}),
    logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in'})
});

    Astcart.List = Ember.Model.extend({
      name: Ember.attr()
    });

    Astcart.Logged_in = Ember.Model.extend({
      id: Ember.attr(),
      logged: Ember.attr(),
      username: Ember.attr(),
      account_id: Ember.attr()
    });

    Astcart.Application.adapter = Ember.FixtureAdapter.create();

    Astcart.Application.FIXTURES = [
        {
            "id"     : "1",
            "logo_url": "img/logo.jpg",
            "logged_in": {
                "id" : "1",
                "logged": true,
                "username": "sac1245",
                "account_id": "4214"
            },          
            "name": "gau",
            "list": [
                        {
                            "id"     : "1",
                            "name": "amit"
                        },
                        {
                            "id"     : "2",                 
                            "name": "amit1"
                        }
            ]
        }
    ];

Template code :

    {{#each item in model}}
        {{item.name}}
        {{item.logged_in.logged}}                               
    {{/each}}

Router code :

  Astcart.ApplicationRoute = Ember.Route.extend({
    model: function() {
      return Astcart.Application.find();
    }
  }); 

Can any one show me how to access data of above node in template?


Solution

  • Your current fixture have the logged_in data embedded:

    Astcart.Application.FIXTURES=[
        {
            ...
            "logged_in": {
                "id": "1",
                "logged": true,
                "username": "sac1245",
                "account_id": "4214"
            },
            ...
        }
    ];
    

    So the logged_in property mapping need the parameter embedded: true, to be able to find this data, as follow:

    Astcart.Application =  Ember.Model.extend({
      ...
      logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in', embedded: true })
    });
    

    Without this it will try to query for the Astcart.Logged_in:

    Astcart.Logged_in.find(1)
    

    and because it doesn't have an adapter, will fail with the exception: Uncaught Error: Ember.Adapter must implement findQuery

    You can see this working in that fiddle http://jsfiddle.net/marciojunior/8zjsh/