Sails API (Ember frontend) rejecting changes - ember.js

I'm getting started with Ember (CLI) and am trying to create a simple CRUD setup, using Sails as my API. Ember's loading all the data as expected, but won't save any changes. I've made some actions buttons to toggle Booleans, and increment a counter, but these just revert back. However, Sails' default attribute "updatedAt" is updated with the date of the attempted update.
It seems GET and DELETE work fine, but am I perhaps missing a step for PUT and POST to work properly?
I'm using the mphasize's sails-ember-blueprints, but haven't read about much trouble with them.
here

It turns out that I had some issues with plurality. The model title in the json payload was singular, and the backend plural. Took a little rejigging, but this was only a simple test project.

Related

How to change users while preserving the store?

I want to implement a "fast login".
I'm developing an enterprise software where a lot of users work in the same organization with the same data in the same computer and I want to be able to know who did what and when. Right now they have to log out and log in and load the data has to be loaded into the store all over again.
What I want is for them to be able to, without logging out, click on a user, from the organization, insert his password and the user is switched while preserving the store.
Any idea how I can accomplish this?
I'm using ember-simple-auth v1.1.0 and ember v2.10.2
The simpliest solution would be disabling page reload when user logs out. As far as I know, it's a reload causes data loss from store, not a logging out by itself. To do this, you need to overwrite sessionInvalidated method in your application route. For example,
sessionInvalidated() {
this.transitionTo('/login');
},
But remember - you lower security with this method: if someone will log out and leave webpage with app open, other person will have a possibility to extract data (if they have enough technical background to at least install ember inspector).
Other solution will require heavy research. I think it should be possible to implement custom authenticator which would allow to authenticate new user without logging out previous, by simply replacing tokens in store. But I don't know how hard it will be to implement and what obstacles you can meet. You will need to read ember-simple-auth's sources a lot, that's for sure.
I was actually able to solve it by simply using authenticate() with another user but never calling invalidateSession() which is the function that calls sessionInvalidated() that looks like this:
sessionInvalidated() {
if (!testing) {
if (this.get('_isFastBoot')) {
this.transitionTo(Configuration.baseURL);
} else {
window.location.replace(Configuration.baseURL);
}
}
}
So by not calling sessionInvalidated() the user isn't redirected or the page refreshed and the new user is able to keep using the store without switching pages.

Unable to push records to store

For some reason, I am unable to push records to the store. I have tried the most basic examples given from the 2.1 docs for the store.push and store.pushPayload examples.
I first tried using:
store.push(store.normalize('myModel', item));
then I tried
store.push({
data: {
id: '666',
type: 'myModel',
attributes: { name: "Bar" },
relationships: { }
}
});
The store.push when used (how I think is correct), returned nothing. I was able to run store.peekAll('myModel').mapBy('name'), and I saw the results of the data I pushed to the store. The data never appeared in the chrome ember inspector. When I used this.store.peekAll('myModel') in my route’s model, no results appeared whereas this.store.findAll('myModel') resulting data would render to my templates and appear in the chrome ember inpsector.
I’m trying to avoid a trip to the server and that is why I want to hydrate the store from data cached in localStorage, and so I’m using peekAll. Any ideas why I’m not seeing data I’m pushing to the store rendering in my templates or why the chrome ember inspector is not showing?
Edit:
I forgot to mention that I am not using the default JSONApi adapter. I am using the most recent version of active-model-adapter with the most recent version of ember-data (2.1 and I tried 2.2-beta3), and ember 2.1. I also have one model that uses LocalForage adapter/serializer and that one also does not work with being pushed to directly. However everything works so long as I use findAll instead of manually push records + peekAll.
Ricardo Mendes helped me figure out what I was doing wrong. I was using the "store:main" container.
App.__container__.lookup('store:main'); // ember 1.x
This does not fly with ember 2.0. The container needs to be "service:store" as in:
App.__container__.lookup('service:store'); // ember 2.x
I was using the API correctly, just pushing to the wrong store.

Elasticsearch self.published?

I am using elasticsearch-rails gem For my site i need to create custom callbacks. https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model#custom-callbacks
But i really confused by one thing. What means if self.published? on this code?
i try to use this for my models
after_commit on: [:update] do
place.__elasticsearch__.update_document if self.published?
end
but for model in console i see self.published? => false but i don`t know what this means
From the document of elasticsearch-rails.
For ActiveRecord-based models, use the after_commit callback to protect your data against inconsistencies caused by transaction rollbacks:
I think it was used to make sure everything is updated successfully into database before we sync to elasticsearch server

Updating a hit counter when an image is accessed in Django

I am working on doing some simple analytics on a Django webstite (v1.4.1). Seeing as this data will be gathered on pretty much every server request, I figured the right way to do this would be with a piece of custom middleware.
One important metric for the site is how often given images are accessed. Since each image is its own object, I thought about using django-hitcount, but figured that was unnecessary for what I was trying to do. If it proves easier, I may use it though.
The current conundrum I face is that I don't want to query the database and look for a given object for every HttpRequest that occurs. Instead, I would like to wait until a successful response (indicated by an HttpResponse.status of 200 or whatever), and then query the server and update a hit field for the corresponding image. The reason the only way to access the path of the image is in process_request, while the only way to access the status code is in process_response.
So, what do I do? Is it as simple as creating a class variable that can hold the path and then lookup the file once the response code of 200 is returned, or should I just use django-hitcount?
Thanks for your help
Set up a cron task to parse your Apache/Nginx/whatever access logs on a regular basis, perhaps with something like pylogsparser.
You could use memcache to store the counters and then periodically persist them to the database. There are risks that memcache will evict the value before it's been persisted but this could be acceptable to you.
This article provides more information and highlights a risk arising when using hosted memcache with keys distributed over multiple servers. http://bjk5.com/post/36567537399/dangers-of-using-memcache-counters-for-a-b-tests

How to get django-simple-history work with Tastypie?

I need to store full history of changes made to objects. I find django-simple-history very appealing but it does not work with django-tastypie. If I send data to API using PUT (update the object), the object is updated OK but the history records are not updated. If I change the objects manually via './manage.py shell' everything works fine. It looks like tastypie is bypassing signals or something.
Any ideas how I could get this to work as expected?
Without seeing your code I'm going to attempt to solve this one analytically.
Looking at django-simple-history it seems the project does indeed create history objects on post_save/post_delete signals and provides access to them using a custom model.Manager subclass.
It looks to me that the resource that TastyPie saves is a ModelResource and not your actual Model instance. This proxy model is aware of the orm and performs all the queries on it.
So what I think happens in simple_history/models.py is that contribute_to_class method declares models.signals.class_prepared.connect(self.finalize, sender=cls) but this signal never fires since TastyPie does not initialize an instance of the model...
This seems so strange and I cannot understand why TastyPie does that, or maybe I'm misunderstanding something? Why don't you try to open an issue in the github repo?