What should I use instead of DS.FixtureAdapter - ember.js

You gotta love ember.js team... I'm getting this depreciation message saying that: "DS.FixtureAdapter has been deprecated and moved into an unsupported addon: https://github.com/emberjs/ember-data-fixture-adapter/tree/master". Guys maintaining that addon advise that we should use a library similar to Pretender. Has anybody done that? Is there a tutorial showing how to integrate this lib so that everything would work as before?

If you are using ember-cli it comes with an http-mock out of the box letting you quickly setup fixtures in a more realistic test scenario. For instance, for a Conversation model you would mock it by running the following prompt on your command line.
ember g http-mock conversations
This will scaffold an endpoint located at server/mocks/conversations.js that your real adapters will use to get data when you run ember serve. You can modify this file to your liking to return whatever fixtures / mock data you want for the various CRUD operations you need.

Server mocks:
Ember CLI's http-mock
Clientside mocks:
Pretender.js
jQuery mockjax
Ember CLI Mirage, basically sugar around Pretender
Clientside mocking has some advantages like portability, making it easy to use in a CI environment, but server mocks let you take advantage of express middleware.
Note: I maintain Mirage. You can watch a screencast & overview here.

I like to use http-mocks with ember-cli. In addition I like to use raw JSON files as the payloads for each endpoint here is an example setup https://github.com/pixelhandler/ember-fixturific/pull/1/files

Related

Does Ember CLI Mirage store data in a database? or the posted data gets removed after refreshing the browser?

I am a newbie to ember and i am using ember cli mirage for servers side API. For example, i want to POST a movie to the movies route, it gets added to the existing one's present in fixtures and is listed in localhost-4200/movies.
On refreshing the browser, the one's posted gets erased and the default one's remain.
Is this the expected behavior ?? Won't the details remain the same after browser refresh or when i reload the next time?
Yes, this is the expected behavior. Fixtures will be applied on pageload.
Mirage was created to let you easily fake your JSON API. It's built on existing libraries, but brings along conventions to make setting up your fake server quick and painless.
Mirage is not a production API, it fakes your API for easy development and testing, but if you need a persistend API, you need to write one by yourself.
If you want to start building a server, but you dont know where to start, take a look at https://github.com/emberjs/data and JSON-API server implementations: http://jsonapi.org/implementations/

Ember.js consuming REST Services

I have been working on a web app using .NET web forms. Now I'm trying to move to a Web API and a client side application. I'm trying to use Ember.js right now. I'm starting to get confused with Ember 2.0, Ember-Data and stuff.
My problem right now is that I'm trying to consume some of the REST services in order to show a list of things in my Ember app. Is it mandatory for me to use Ember-Data? If not, how can I consume the services to create, read, update and delete items (I mean, am I able to use only ajax or something)?
The Web API was built in order to be usable for many technologies. I need to build an specific URL for each request (even a simple GET has some mandatory parameters). That's why ember-data is not working well for me, at least not what I have learnt about it. Because this.store.find won't allow me to build the URL I need.
Greetings.
Is it mandatory for me to use Ember-Data?
No, it's not. You can use other libraries such as Ember RESTless, or write your own.
You can even use simple jQuery AJAX calls whenever you need to fire a request to your API, however, you'll loose many benefits of Ember Data, such as lazy, asynchronous loading of models when they are needed.
That's why ember-data is not working well for me, at least not what I
have learnt about it. Because this.store.find won't allow me to build
the URL I need.
You can override buildURL method of adapter you're using(for example RESTAdapter), source code on GitHub could be helpful if you would like to do that, and you can find everything you need to create your own behavior for buildURL method in Ember API documentation.

How to call a parse-cloud function in ember.js controller?

I am working on a ember-cli app which fetch data from parse-cloud using ember-parse-adapter . It was working fine until recently i need to call a parse-cloud function in my ember controller. So, is there a way that i could use ember-parse-adapter to call parse-cloud function or any other approach to do that.
I don't know of a way with the ember-parse-adapter BUT:
You can make your own AJAX calls via jQuery $.ajax and the Parse REST API
You can use the npm package for Parse.
npm install parse
Go to node_modules/parse/build and copy parse-latest.js to your /vendor directory
Import into your app using Brocfile.js - app.import('vendor/parse-latest.js').
The Parse namespace and SDK are now available everywhere in your app.
Keep in mind both these approaches have security concerns as you will be making your API key available to anyone who inspects your app.

Ember CLI http-mock as Addon

I'm using Ember CLI's http-mock feature to mock REST API endpoints, but I'd like to use it in multiple Ember CLI applications. I thought an addon would be a great solution to this, but I can't seem to get it to work. Does Ember Addon support http-mock?
Here's what I did.
Created an add on
$ ember addon my-http-mock
Then I created a simple test endpoint in the addon
$ ember g http-mock users
After publishing it to my github repository, I imported it into an Ember CLI project like this in package.json
"dependencies": {
"my-http-mock": "git://github.com/git-username/my-http-mock"
}
After npm installing it, I ran my app, but going to http://localhost:4200/api/users doesn't go to the API endpoint, and instead tries to load the Ember app.
Is there any way to use http-mock in multiple applications?
You'll need to have your addon implement the serverMiddleware and you can add middleware, or routes, to the http-mock running in the consuming ember-cli application.
Advanced Addon customization in ember-cli docs
That hook get's passed a config object that has the express app instance on it at config.app. You can then add whatever you'd like to do. If you're using the generated http-mock in the addon, it'd look something like this
{
name: 'my-http-mock',
serverMiddleware: function(config) {
// To require ALL mocks from your addon
var server = require('./server');
server(config.app);
// To require individual mocks
var users = require('./server/mocks/users');
users(config.app);
}
}
This is untested code but should work. The require all mocks one could possibly conflict in a weird way because it adds the bodyparser middleware and connect-restreamer and it probably already has that included if your app already has those from it's local http-mock. Try it out though! :)
note: This answer is in reference to using ember-cli 0.1.2

Sails.js and Ember.js integration

Has anyone tried to use sails as ember's backend API?
How would you go about it?
Is there an example available?
Since Sails has a different format in its built-in blueprints, is it better to make sails rest json format confirm to what ember expects or the other way around?
Thanks!
Edit
So there are two approaches:
change the sails format, maybe with: CRUD blueprint overriding in sails.js
change what ember expects: http://discuss.emberjs.com/t/sails-js-as-the-backend/5233
There is a project called Sane Stack that uses a Sails blueprint to generate the appropriate setup for Ember and Sails, as well as provides a generator for creating resources in Ember and Sails at the same time.
QUICKSTART
npm install -g sails sane-cli
sane new project --docker creates project with sails-disk and Docker. For production databases see Options.
sane generate resource user name:string age:number to generate a new API on the backend and models on the frontend
sane up to start the sails server on localhost:1337 as well as the ember dev server on localhost:4200.
To work on your frontend-app you work as you would normally do with ember-cli on localhost:4200.
You are now good to go.
Note: If you use Docker, make sure you have fig installed. On Mac or Windows also boot2docker and for Linux see: https://docs.docker.com/installation/ubuntulinux/
I can't speak for the "change what Ember expects" option, but I can say that this is precisely what blueprint overrides in Sails were invented for. This would be a perfectly reasonable way to make your app Ember-compatible.
Also keep in mind that all of the CRUD blueprints use the res.ok() response to actually send data down to the client; if you like, you can just override that with your own api/responses/ok.js, perhaps using req.url to determine the resource that was being requested, and use that to wrap your JSON object in the manner that Ember expects.
I just put up a few blueprint overrides that can be used as a starting point for using Sails as an Ember Data backend: https://github.com/mphasize/sails-ember-blueprints
Please let me know if you find a bug or have some suggestions for improvements!
Haven't used ember in a few months, but the default blueprints responses are not compatible with ember-data.
Ember seems to be trying to conform to the jsonapi spec.
Sails default blueprint responses do not.
That being said, it would be pretty easy to roll your own responses with sails.