Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I’m new working with Ember.js and I’m confused about a few things. My real issue is working with MySQL and ember-data. I’ve worked through a few tutorials and found this youtube very useful. At this point, I can create an app that runs on top of a Node.js server. The node server communicates with a mysql database and creates a RESTful API that the ember app consumes. I have no problem getting data from the mysql database and displaying it. But I cannot figure out how to save or update data in the MySQL database. I vaguely understand the concept of the ‘store’ and can push objects into it and display them, but that's it. Are there any examples out there of how to write or update a mysql database using ember-data and Node.js?

EDIT: this question is really asking about the structure of an ember-mysql app and what has worked in production for others.

This is an example of pushing into the store in the routes/message.js

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        var self = this;
        setTimeout(function(){
            console.log("done waiting...")

            self.store.push({
                data: [{
                    id: 11,
                    type: 'message',
                    attributes: {
                        message: 'New Message'
                    },
                    relationships: {}
                }]
            });
        },1000);
        return self.store.findAll('message');
    }
});
share|improve this question

From ember-data perspective it doesn't matter if you use mysql or any other database engine. Ember-data only communicates with an API - a RESTful JSON API by default.

So the node.js part of your application should implement endpoints for both reading and saving data. However writing this API from scratch is a waste, it'd be much easier if you used a framework - if you wish to stick with node.js check out Sails.js, if not, give Ruby on Rails a try.

In both of those frameworks it's pretty straightforward to generate a working RESTful API that ember-data could consume.

As for your code example: Store is a layer in ember-data that's responsible for managing record's data in browser memory. Pushing to Store only adds record to memory (technically records are pushed to Store when you read them from the API).

In order to save a model, just call model.save() which sends a POST (when creating) or a PUT (when updating) request to the API - at least with default JSON API Adapter. (Adapter is the layer that understands how your API works and communicates with it.)

If you're just building a prototype, give Firebase a try - that's basically a ready backend for your JavaScrip app. You could than ditch the node.js part at all - at least for the time beeing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.