Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember adds empty row to data with slug as id

I have a resource where I list all my projects from server /projects. You can visit specific project going to /projects/:slug.

When I visit projects resource I see this data in Ember Inspector:

/projects

id  | Slug              | Title                 |
-------------------------------------------------
1   | first-project     | First project         |
-------------------------------------------------
2   | second-project    | Second project        |
-------------------------------------------------
3   | third-project     | Third project         |

When I visit a project from list of projects I get the same data without new ajax request and everything is working fine. /projects/first-project

The problem comes when I refresh /projects/first-project page. Ember makes an ajax request to fetch data from server but It also inserts an empty row with slug as id to data.

id              | Slug              | Title                 |
-------------------------------------------------------------
first-project   |                   |                       |
-------------------------------------------------------------
1               | first-project     | First project         |

Now visiting project list It is showing a list of projects but at the top of the list is this empty row. Why it is inserting this slug row to my data? Maybe my logic is wrong.

<li>
    <a id="ember451" class="ember-view" href="/projects/undefined">
        <script id="metamorph-13-start" type="text/x-placeholder"></script>
        <script id="metamorph-13-end" type="text/x-placeholder"></script>
    </a>
</li>

My code for projects:

App.Router.map(function() {
    this.resource('project', { path: '/projects' }, function() {
        this.resource('project.show', { path: ":post_slug"});
    });
});

App.Project = DS.Model.extend({
    slug: DS.attr("string"),
    title: DS.attr("string")
});

App.ProjectIndexRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('project').then(function(data) {
            return data;
        }); 
    }
});

App.ProjectShowRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('project', params.post_slug).then(function(data) {
            return data;
        }); 
    },
    serialize: function(model) {
        return { post_slug: model.get("slug") };
    }
});

I'm using:

DEBUG: ------------------------------- 
DEBUG: Ember      : 1.1.2 
DEBUG: Ember Data : 1.0.0-beta.3 
DEBUG: Handlebars : 1.0.0 
DEBUG: jQuery     : 1.9.1 
DEBUG: ------------------------------- 
like image 235
Mr. Sensitive Avatar asked Nov 01 '22 11:11

Mr. Sensitive


1 Answers

Adding this helps. now primary key is slug for Project model and it doesn't duplicate rows.

App.ProjectSerializer = DS.RESTSerializer.extend({
    normalize: function(type, hash, property) {
        hash.id = hash.slug;
        return this._super(type, hash, property);
    }
});
like image 173
Mr. Sensitive Avatar answered Nov 08 '22 03:11

Mr. Sensitive