Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the hasMany relations get removed on reload #38

Closed
oblongta opened this issue Feb 14, 2014 · 12 comments
Closed

the hasMany relations get removed on reload #38

oblongta opened this issue Feb 14, 2014 · 12 comments

Comments

@oblongta
Copy link

I have define one to many relationship between company and person.The records are successfully added and saved.However on reload the hasMany arrays content gets wiped out and hence all the relations get lost.

I am using ember-data-beta-1 and ember-data local storage adapter.
And my code is:

App = Ember.Application.create({});
App.Store = DS.Store.extend({
    adapter: DS.LSAdapter
});
App.Company = DS.Model.extend({
    name: DS.attr('string'),
    turnover: DS.attr('string'),
    employees:DS.hasMany('person')   
});
App.Person = DS.Model.extend({
    name: DS.attr('string'),
    department: DS.attr('string'),
   company:DS.belongsTo('company') 
});
App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('company');
    }
});
App.ApplicationController=Ember.ArrayController.extend({
 add:function(){
 this.store.createRecord('company',{
 name:'xyz',
 turnover:'abc'
}).save();
},
addperson:function(model){
var a=model.get('employees');
a.createRecord({
 name:'pqr',
 department:'lmn',
 company:model
}).save();
}
});

and my index.html file is:

<title>json method#1</title>
<script type="text/x-handlebars">

    <div id="notes" class="azureBlueBackground azureBlueBorderThin">
        <button class="btn btn-default btn-xs" {{action "add"}}>New Company</button>
        <div class="list-group" style="margin-top: 10px;">
            {{#each controller}}
            <button class="btn btn-default btn-xs" {{action "addperson" this}}>employee</button>
            <h1>  {{name}} {{turnover}}</h1>
             {{#each employees}}
             {{name}} {{department}}
             {{/each}}
            {{/each}}
        </div>
    </div>
</script>
<script src="js/libs/jquery-1.10.2.js"></script> <script src="js/libs/handlebars-1.1.2.js"></script> <script src="js/libs/ember-1.3.1.js"></script> <script src="js/libs/ember-data-beta-1.js"></script> <script src="js/libs/ember-data-localstorage.js"></script> <script src="js/app.js"></script>
@bjarkehs
Copy link
Contributor

The newest version of the localstorage-adapter is meant for Ember Data beta 5 and onwards. I would consider upgrading. Also make sure you have the newest version of master.

@oblongta
Copy link
Author

i tried with latest version of ember-data(i.e. ember data beta 6) and even latest version of master but having same problem
is this a bug or some conceptual mistake by me??

@kurko
Copy link
Collaborator

kurko commented Feb 14, 2014

@oblongta I believe that if reload is broken, it is in Ember Data rather than in the adapter. Nonetheless, try doing company.set('person', person) instead of using it in createRecord().

Also, this is wrong:

// you need to use the store to create a record,
// not a model (which is an array in this case)
var a=model.get('employees');
a.createRecord();

Do this instead

var person = this.store.createRecord('person', {
  name: 'pqr',
  department: 'lmn',
});

person.save().then(function(person) {
  person.set('company', model);
  person.save();
});

I ask you to do this because I recently had problems with embedding relations in createRecord().

@oblongta
Copy link
Author

ok then how do i add person to a company which has hasMany relation with the person??
And The problem is not creating a record or saving it.
The records are created and they are even getting saved but the relation between person and company gets lost i.e. hasMany arrays content gets wiped out .
It is related to the issue mentioned below as i think:
#23

And my code is update version of this jsbin if you look closely:
http://jsbin.com/oKuPev/72/edit

@bjarkehs
Copy link
Contributor

This is how I do it for a project of mine:

var order = this.get('model');
var user = App.get('currentUser');
order.set('user', user);
order.save().then(function(o) {
    user.get('orders').addObject(order);
    user.save().then(function(u) {
        this.transitionToRoute('order.handle', order, getDateString());
    });
});

Don't get confused with my use of App.get('currentUser') just think of it as your person. You could potentially have created this person already. But the above code saves the records and maintains the hasMany relationship.

My relationships are:

App.User = DS.Model.extend({
    username: DS.attr('string'),
    password: DS.attr('string'),
    orders: DS.hasMany('order')
});

App.Order = DS.Model.extend({
    navision: DS.attr('number'),
    closed: DS.attr('boolean'),
    user: DS.belongsTo('user'),
    hours: DS.hasMany('hour'),
    materials: DS.hasMany('material'),
    services: DS.hasMany('service')
});

@oblongta
Copy link
Author

Thanxx,
I will look and hope it to get working.
can you tell me whats wrong in this implementation:
http://jsbin.com/oKuPev/72/edit

and my error is same as #23 as it seems to me.
Any method to overcome that

@oblongta
Copy link
Author

I tried this and getting the same problem:
App.ApplicationController=Ember.ArrayController.extend({
add:function(){
var c= this.store.createRecord('company',{
name:'abc',
turnover:'lmn'
});

var p=this.store.createRecord('person',{
name:'xyz',
department:'pqr'
});

p.save().then(function(p){
p.set('company',c);
p.save().then(function(p){
c.get('employees').addObject(p);
c.save();
});
});
}
});

@oblongta
Copy link
Author

screenshot from 2014-02-14 22 05 30
this is screen shot before reloading.
You can see person attributes in the template namely xyz and pqr.
Also,in properties tab you see content property has that person record in the array.
However on reloading i get this:
screenshot from 2014-02-14 22 06 06
Now the content property doesnt contain that person record in array and hence no attributes of person are rendered in template.
But however on checking local storage i find that the person exists in local storage:
image

@bjarkehs
Copy link
Contributor

Ah, now I remember why. I'll have to take a look at it. But try using findAll instead of find.

@oblongta
Copy link
Author

no findAll doesnt solve the issue.
Is it issue with ember data/local adapter or some conceptual error by me??

@bjarkehs
Copy link
Contributor

Okay, I know now. As the README states:

Include localstorage_adapter.js in your app and then like all adapters:

App.ApplicationSerializer = DS.LSSerializer.extend();
App.ApplicationAdapter = DS.LSAdapter.extend({
    namespace: 'yournamespace'
});

In the code you linked initially there is no ApplicationSerializer. This serializer handles all the relationships, so without it you're never going to get the relationships.

I will have a closer look at find() since I still expect it to be a little buggy, so use findAll for now.

@oblongta
Copy link
Author

Thanks for help.
it solved my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants