This is my code (using ember-cli):
app.coffee
`import Ember from 'ember'`
`import Resolver from 'ember/resolver'`
`import loadInitializers from 'ember/load-initializers'`
Ember.MODEL_FACTORY_INJECTIONS = true
App = Ember.Application.extend
modulePrefix: 'dashboard' # TODO: loaded via config
Resolver: Resolver
loadInitializers App, 'dashboard'
`export default App`
adapters/application.coffee
`import DS from 'ember-data'`
ApplicationAdapter = DS.FixtureAdapter.extend()
`export default ApplicationAdapter`
models/mod.coffee
`import DS from 'ember-data'`
Mod = DS.Model.extend
name: DS.attr 'string'
body: DS.attr 'string'
summary: DS.attr 'string'
category: DS.attr 'string'
Mod.reopenClass {
FIXTURES: [
{
id: 1
name: "First mod"
body: "..."
summary: "..."
category: "api"
},
{
id: 2
name: "Second mod"
body: "..."
summary: "..."
category: "api"
}
]
}
`export default Mod`
But in the app nothing is thrown when I run ember serve nor in the browser conole (output:
DEBUG: ------------------------------- vendor.js:27630
DEBUG: Ember : 1.7.0 vendor.js:27630
DEBUG: Ember Data : 1.0.0-beta.8.2a68c63a vendor.js:27630
DEBUG: Handlebars : 1.3.0 vendor.js:27630
DEBUG: jQuery : 1.11.1 vendor.js:27630
DEBUG: ------------------------------- vendor.js:27630
generated -> route:application Object {fullName: "route:application"} vendor.js:27630
generated -> route:index Object {fullName: "route:index"} vendor.js:27630
generated -> controller:application Object {fullName: "controller:application"} vendor.js:27630
Rendering application with default view <dashboard#view:toplevel::ember323> Object {fullName: "view:application"} vendor.js:27630
generated -> controller:index Object {fullName: "controller:index"} vendor.js:27630
Rendering index with <dashboard#view:index::ember339> Object {fullName: "view:index"} vendor.js:27630
Ember Debugger Active
)
In ember inspector it shows me my mod model but with no record
Here is the whole github repo https://github.com/OpenCubes/dashboard/tree/temp
ok 2 things.
first you must generate a route if you are fetching the whole collection. Our actively generated routes don't do this by default. I believe it just has to do with ambiguity and that we don't want to fetch your entire datasource by accident, so we leave this up to the user todo.
// app/routes/mods.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() { return this.store.find('mod'); }
});
second you must use reopenClass when providing fixtures. This is because IE6 + there is not way to properly propagate static/class variables to descendants. So ember implements its own mechanism. At some future point in time setPrototypeOf will be usable and we can defer to that. Until then for ember-cli apps, please use embers sanctioned way.
import DS from 'ember-data';
var Mod = DS.Model.extend({...})
Mod.reopenClass({
FIXTURES: [
...
]
});
export default Mod;
then it works
Update
Hi, I have moved that code to cli and basically, all I had to do was:
create an adapter for Mod:
C:\>ember g adapter mod
Then I went to the source of the generated Mod adapter, and changed the declaration to extend from FixtureAdapter instead of RESTAdapter.
import DS from 'ember-data';
export default DS.FixtureAdapter.extend();
Also, had to change the model declaration, and it's pretty much the same as yours now.
No need to create store or anything.
I don't know really if this is why you're having trouble, but, did you override the adapter in your store? I suspect it might not be using your adapter.
I've made a quick sample (here) that you can use as reference.
Note in the sample below that I'm passing the fixture adapter name as a string to the store (there are other ways to do this as well).
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Store = DS.Store.extend({
// override default adapter
adapter: 'ApplicationAdapter'
});
[ ... other code ... ]
App.Colour = DS.Model.extend({
name: DS.attr('string'),
hex: DS.attr('string')
});
// settings the fixtures directly in the model class
App.Colour.FIXTURES = [
{id: 1, name: 'red', hex: '#F00'},
{id: 2, name: 'green', hex: '#0F0'},
{id: 3, name: 'blue', hex: '#00F'}
];
Then in my route I query normally:
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('colour');
}
});
This should output the fixture records normally.
Related
Wow this is hard to find.
I have an existing model in ember and I would like to add a new column. I have't been able to see how to generate this from the CLI, so have manually added it to my component.js and models/post.js. I've added the field to my form and the handlebar to my view. Checking Firebase I can confirm I'm not updating the field.
In Rails I would simply run rails generate migration add_snippet_to_posts snippet:string but doing this in Ember just creates a new model.
model/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
createdDate: DS.attr('date'),
text: DS.attr('string'),
snippet: DS.attr('string') #<=manually added this.
});
component.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
createPost: function (model) {
this.sendAction('createPost', model);
// Clear each input field
this.set('newPost.title', null);
this.set('newPost.author', null);
this.set('newPost.text', null);
this.set('newPost.snippet', null); #<= manually added this
}
}
});
How do I do this?
Solved
Needed to update routes/index.js too:
import Ember from 'ember';
export default Ember.Route.extend({
model: function () {
return this.store.findAll('post');
},
actions: {
createPost: function (model) {
let post = this.store.createRecord('post', {
title: model.title,
text: model.text,
author: model.author,
snippet: model.snippet, # <= add this too
createdDate: new Date()
});
post.save();
}
}
});
The official answer would be that you cannot just add an attribute with ember-CLI to a model that has already been created - and at the same time, update everything it may effect throughout your app. You have to manually write the attributes and how they are used in routes/components/templates etc.
That sounds awesome that Rails can just know all that stuff. : )
My ember app is not sending my foreign key to the back-end.
I have a table called issues which is has a related table called categories
My model is:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
category_id: DS.belongsTo('category'),
description: DS.attr('string')
});
My route is:
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('issue');
},
actions: {
create: function(){
var issue = this.store.createRecord('issue');
issue.name = this.get('controller').get('newName');
issue.description = this.get('controller').get('newDescription');
issue.category_id = parseInt(this.get('controller').get('newCategory'));
//debugger;
console.log(issue);
issue.save();
},
...
other actions
...
}
}
});
the console.log from above looks like the category_id is getting set correctly:
category_id: 3
description: "foobar"
name: "test"
However my JSON payload that gets sent to the backend looks like:
{"issue":{"name":"test","description":"foobar","category_id":null}}
I tried stepping through by adding a custom serialiser in app/serializers/application.js
export default DS.RESTSerializer.extend({
...
serialize: function(snapshot,options){
console.debug('options='+options);
debugger;
var json = this._super(snapshot, options);;
return json;
}
...
});
But I got lost in all the super calling super indirection.
The snapshot.record has category_id: 3, but the json coming back from the this._super() call has category_id: null
options has includeID:true
Any clues will be much appreciated ...
Ember : 2.0.2
Ember Data : 2.0.0
Your model definition is wrong, when dealing with relationships you define them just as you would define any other attribute, there is no need to use _id.
export default DS.Model.extend({
name: DS.attr('string'),
category: DS.belongsTo('category'),
description: DS.attr('string')
});
As for the creation you should always use setters/getters when dealing with ember objects:
create: function() {
var issue = this.store.createRecord('issue', {
name: this.get('controller').get('newName'),
description: this.get('controller').get('newDescription'),
category: this.get('controller').get('newCategory') // assuming new category is a DS.Model instance of category
});
issue.save();
}
If you wish to stick to the syntax you have you would use issue.set('name', this.get('controller').get('newName')), from the looks of your code it seems you are going about this in the wrong way.
You should have a this.route('new') nested under your issues route, that way you wouldn't have to use the controller to store information.
You would simply set the model of the new route to:
model: function() {
return this.store.createRecord('issue');
}
Your template would make use of the input helpers like so:
{{input value=model.name}} and your action would just get the currentModel and call .save().
I am new in Ember.js and I am having trouble connecting my app to the backend data.
I am currently using ember data v.2.1, ember v.2.1, ember inspector v.1.9.3
We are upgrading an existing app and we want to use ember as front-end framework. The app right now is java based and is using xsl on the front.
What I need to do is to connect through HTML request with ember to the existing backend which return XML and convert it to json so I can use it in ember. I tried to follow this example : https://bendyworks.com/old-new-soap-ember-js/ without any luck.
With the inspector I can see my model, but I can't load any data in it.
I have a model that look like this:
// app/models/get-menu.js
import DS from 'ember-data';
export default DS.Model.extend({
description: DS.attr('string'),
url: DS.attr('string')
});
I have an adapter:
//adapters/menu.js
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
headers: {
'Content-Type': 'text/xml;charset=UTF-8'
},
findAll: function() {
return this.ajax('LinkForHTTPRequest','POST');
},
ajaxOptions: function(url) {
// pretend to be a 'GET' to avoid certain nastiness in default impl
var hash = DS.RESTAdapter.prototype.ajaxOptions.call(this, url);
hash.type = 'GET';
hash.dataType = 'xml';
return hash;
}
});
and I have a serializer :
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONSerializer.extend({
extractArray: function(store, xmlDoc) {
var $ = Ember.$;
var xml = $(xmlDoc).find('TMSMENUS').text();
var innerXml = $($.parseXML(xml)).find('MENUS');
return innerXml.map(function(idx, el) {
return {
id: idx,
name: $(el).find('description').text(),
url: $(el).find('url').text(),
};
}).get();
}
});
I am using ember cli with ember data and have been piecing together information but it still doesn't work. This all involves the Home model, route and template. I feel like I'm close but still no cigar. I took everything out except for the title to simplify it. According to documentation I've read, everything is as it should be.
here is my app.js
import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
import config from './config/environment';
Ember.MODEL_FACTORY_INJECTIONS = true;
var App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver
});
loadInitializers(App, config.modulePrefix);
export default App;
Here is my home model:
import DS from 'ember-data';
export default DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
Here is my home route:
import Ember from 'ember';
//import DS from 'ember-data';
export default Ember.Route.extend({
model: function(){
return this.store.find('Home');
},
});
This is my home template:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
{{outlet}}
Could someone please help me out?
I think it has something to do with my model hook.
Could this also be a controller issue. I generated a basic controller. Should I have generated an arrayController?. The home route when saved gives me this error message:
models/home.js: line 9, col 1, 'Home' is not defined. but when I define it there still is a problem. Am I supposed to ad a folder called adapters, then put a file in it called application.js, then ad export default DS.FixtureAdapter.extend(); . But when I do that it tells me DS is not defined
It looks like you are explicitly exporting your model as Home and then you are trying to find('home') which does not have the same letter case.
Ember will not automatically resolve the case for you. If you want to use Home as the model, you will need to call it in the same way every time.
You might even need to import Home from '../models/model-file'; if you want to call it from the route..
An easier thing to try would be to use the implicit export default Ember.Model.extend({}) and let ember-cli resolve the class using your file name.
http://www.ember-cli.com/#using-modules
Your template seems to be the issue. You reference an item property that isn't there:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
Also, you have some syntax problems with your model. Try this:
import DS from 'ember-data';
var Home= DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
export default Home;
For the Fixture Adapter you are correct in adding the application.js to the adapters folder and import DS from 'ember-data'.
Also in Steve H's example of your home model, the definition of the FIXTURES is not correct. Try using:
Home.FIXTURES = [
...
];
Make sure the pathing is correct.
I am working on my first ember application using Ember-cli
Here i want to add feature of image-upload using cloudinary_js
Referring this link
Image Model:
import DS from 'ember-data';
var attr = DS.attr;
export default DS.Model.extend({
imageUrl: attr('string'),
thumbImageUrl: attr('string'),
standardImageUrl: attr('string'),
favourite: attr('string'),
order: attr('number')
});
I have already added the required js files using bower and listed them in Brocfile.js
app.import('vendor/jquery-ui/jquery-ui.js');
app.import('vendor/jquery.iframe-transport/jquery.iframe-transport.js');
app.import('vendor/blueimp-file-upload/js/jquery.fileupload.js');
app.import('vendor/cloudinary_js/js/jquery.cloudinary.js');
Added file field as component:
import Ember from "ember";
export default Ember.View.extend({
tagName: "input",
type: "file",
accept: "image/*",
class: "cloudinary-fileupload",
dataCloudinaryField: "image_id",
attributeBindings: [ "name", "type", "value", "class"],
change: function() {
}
});
Here am stuck with, where to specify the config of cloudinary (cloud name and api key)?
Can anyone please help me with the detailed steps of cloudinary integration with ember using ember-cli.
Thanks.
You'll want to have an initializer that sets the cloud_name and api_key properties. I would put this code in app/initializers/cloudinary.js.
export default {
name: 'cloudinary',
initialize: function() {
$.cloudinary.config({
cloud_name: 'MYCLOUD',
api_key: 'MYKEY'
});
}
};
I wrote a detailed blog post on how to integrate cloudinary_js and Ember a few days ago. There's an example app that uses ember-cli and shows how to configure Cloudinary and everything you'd need to get it up and running. There are some tricky parts to getting the whole thing working, so I'd recommend checking that out if you get tripped up after the config setup right.