Ember not saving records when using DS.RESTAdapter - ember.js

I'm trying to create a new object via the DS.RESTAdapter , but I don't see any activity on the backend (via console or in the db). There's no errors output in the console (using chrome). Reading data via index and show works fine. In my route.rb I have
resource #posts, only: [:index, :show, :new, :create]
After I fill out a form and click submit this line gets called (trying static data to start with)
this.get('store').createRecord('post', {name: 'asdf'});
In the ember inspector in chrome I see under "Data" the new records being created without the ID. I notice the new method in my backend post controller isn't called so is there another step I have to do for the adapter to attempt a REST PUT?
Thanks!

createRecord only creates the record in the store.
To persist that record you need to call save() on it.
// Puts record into store
var newPost = this.get('store').createRecord('post', {name: 'asdf'});
// Persists it
newPost.save();
See: http://emberjs.com/guides/models/persisting-records/

Related

Ember save data to store and display without having a server api

I have a users model which fetches data from github users api (https://api.github.com/users). While displaying the list there is a add button which should add the user to a shortlist section below and which has a remove button to remove user from shortlist. I don't have api to save shortlist data. What is the best approach to make this work?
Try 1: Created a shortlist model and used store.push
this.store.push({
data: [{
id: user.id,
type: 'shortlist',
attributes: {
login: userData.login,
avatar_url: userData.avatar_url,
type: userData.type
}
}]
});
and used item.unloadRecord(); to remove from model. But did nor found a way to fetch all record and show as this.store.peakAll('shortlist') wasen't working.
Try 2: Used localstorage to add user to shortlist, display and remove but here it needs page reload to display the add/remove changes as i used setupController to get the items from localstorage.
Please suggest how to do this in best possible way.

JSONAPI strong params with Rails and Ember

I'm using Ember with ember-data and a rails api. I had a createRecord() and save() for the record that was working fine. The payload in the network tab for the post request to create the record in rails looks like: {data: {attributes: { foo: 'bar' } }.
In the rails controller, I have strong params like so: params.require(:data).require(:attributes).permit(:foo), which was working fine for a little while. Now when I send the request rails says that param is missing or the value is empty: data. If I look in the network tab in the browser the payload for the request still looks the same as stated above. If I puts params it only shows {"controller": "api/v1/answers", "action": "create"} and isn't showing the data payload at all.
Is there any reason why rails isn't picking up on the right params from ember now? I did try to add an association to the model that I'm trying to create, which is when it started failing. However, I rolled back to when it was working, but it's not working anymore.
I fixed this by going into the config/initializers/mime_types.rb file in the rails api and changing the file to look like:
api_mime_type = %W(
application/vnd.api+json
text/x-json
application/json
)
Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_type

How to update an existing record when creating a record with ember-data

When I try to create a model instance
record = this.store.createRecord('model', { /* whatever */ });
record.save();
And my API updates an already existing backend record instead of creating a new one. The API returns HTTP 200 [ok] (could also be 202 [accepted]) instead of 201 [created].
What is the ember way to have this record not created in the store if an instance of the same record is already there?
Right now if I "create" a record that turns out to update an existing record X times, I end up having the same record (with the same ID) repeated X times in my ember-data store.
When you use createRecord, you're telling Ember to add a new record to your store.
You need to fetch your record into your store first, if you want to update it:
this.store.find('model', id).then(function(record) {
record.set('property', 'value');
record.save();
});
http://emberjs.com/api/data/classes/DS.Store.html#method_createRecord
Maybe you're looking for this.store.update( ... ), depending on your specific needs: http://emberjs.com/api/data/classes/DS.Store.html#method_update

Trigger action in Rails controller on model callback

I am trying to wrap my head around Live Streaming with Server-Sent Events in Rails. I have a Rake task listening for file changes which adds records to the database. Once added I would like to send a SSE to the frontend.
But, the model can't send events to the frontend, the controller is responsible for that. How do I tell my controller a new record was added to the database?
My (broken) solution so far: use an EventBus with an after_save callback in the model that announces the changes and asks the controller to listen for these messages:
require 'reloader/sse'
class SseController < ApplicationController
include ActionController::Live
def index
response.headers['Content-Type'] = 'text/event-stream'
sse = Reloader::SSE.new(response.stream)
EventBus.subscribe(:added) do |payload|
sse.write({ /* payload */ })
end
rescue IOError
ensure
sse.close
end
end
I think my request ends before the event is received meaning it will never end up in de subscribe block. Is this the right approach, if so, what am I missing?
SOLN 1: you can use rest_client gem to send a request to your controller in your after_save callback from model.
SOLN 2: Why dont you call a model method in your controller which creates the database records and then handles further action based on whether record was created or not

myModel.save() => rejected promise with status 201

When I call myModel.save(), in one of my controllers, to insert a new record into the store I get back a promise with isRejected: true.
The reason object has the following attributes:
readyState: 4,
status: 201,
statusText: "created"
The object is created properly in my backend REST service. In fact, if I put the transitionToRoute in the catch(), instead of the then(), everything would appear to be just fine.
What's going on here?
You need to return the object back with your request. This is particularly important because the server should provide an id for that newly created record. Without an id there is no definitive way of updating and being sure you're updating the correct record. The format should follow the same format if you were to find the model.
IE:
{
type: {
id:12312,
property:'value',
otherProperty:'value'
}
}