here is the adapter In Ember Data, an Adapter determines how data is persisted to a backend data store. Things such as the backend host, URL format and headers used to talk to a REST API can all be configured in an adapter.
/* adapters/application.js */
import FirebaseAdapter from "emberfire/adapters/firebase";
export default FirebaseAdapter.extend({});
A Controller is routable object which receives a single property from the Route .. here is the controller
/* controllers/cars.js */
import Controller from "@ember/controller";
export default Controller.extend({
actions: {
deleteCar(id) {
this.get("store")
.findRecord("car", id, { reload: true })
.then(car => {
car.destroyRecord();
car.save();
//self.transitionToRoute("cars");
});
}
}
});
/* controllers/cars/edit.js */
import Controller from "@ember/controller";
export default Controller.extend({
actions: {
editCar: function(id) {
var self = this;
var make = this.get("model.make");
var model = this.get("model.model");
var year = this.get("model.year");
this.store.findRecord("car", id).then(function(car) {
car.set("make", make);
car.set("model", model);
car.set("year", year);
car.save();
self.transitionToRoute("cars");
});
}
}
});
/* controllers/cars/new.js */
import Controller from "@ember/controller";
export default Controller.extend({
actions: {
addCar: function() {
var self = this;
var rand = Math.floor(Math.random() * 10000 + 1);
var newCar = this.store.createRecord("car", {
id: rand,
make: this.get("carMake"),
model: this.get("carModel"),
year: this.get("carYear")
});
newCar.save();
self.transitionToRoute("cars");
}
}
});
In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name .. here is the model
/* models/cars.js */
import DS from "ember-data";
export default DS.Model.extend({
make: DS.attr("string"),
model: DS.attr("string"),
year: DS.attr("string")
});
In Ember, when we want to make a new page that can be visited using a URL, we need to generate a "route" using Ember CLI .. here is the routes
/* routes/cars.js */
import Route from "@ember/routing/route";
export default Route.extend({
model() {
return this.store.findAll("car", {
orderBy: "make"
});
}
});
/* routes/cars/edit.js */
import Route from '@ember/routing/route';
export default Route.extend({
});
/* routes/cars/new.js */
import Route from "@ember/routing/route";
export default Route.extend({
model() {
return this.store.findAll("car");
}
});
The EmberRouter class manages the application state and URLs .. here is the router
/* router.js */
import EmberRouter from "@ember/routing/router";
import config from "./config/environment";
const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route("cars", function() {
this.route("new");
this.route("edit", { path: "/edit/:car_id" });
});
this.route("users");
});
export default Router;
Aucun commentaire:
Enregistrer un commentaire