ember-cli (2.4.3) not generating DS models - ember.js

I am very surprised not to get DS models upon generating ember-cli model
ember -v
ember-cli: 2.4.3
node: 5.10.1
os: darwin x64
ember g model rental
Then I get :
// app/models/rentals
import Model from 'ember-data/model';
export default Model.extend({
});
according to the ember guide , I should get :
// app/models/rentals
import DS from 'ember-data';
export default DS.Model.extend({
});
what could be wrong ? thanks for feedback

There is nothing wrong with the generated code :)
Ember Data has been made an addon, and part of that work was to tweak the public ES6 modules so everything doesn't hang off of the DS object. Now you can directly import the Model like in the first code sample you show.
I have opened an issue on the Guides repository to fix the Tutorial section of the guides.

The new generated code should look like the following,
import Model from 'ember-data/model';
import attr from 'ember-data/attr' ;
export default Model.extend({
title: attr('string'),
owner: attr('string')
});

Related

ember-pouch: does not load hasMany relationship when the belongsTo side is polymorphic

I have an ember-data app that uses the ember-pouch adapter for local & remote storage.
I am unable to load hasMany relationships when the belongsTo side is polymorphic. I've played with async: true/false and donstsave: true/false options on the hasMany side, to no avail.
The setup:
post can have many comments.
comment can have many comments.
comment belongs to commentable.
// app/models/post.js
import DS from 'ember-data';
import { Model } from 'ember-pouch';
export default Model.extend({
DS.hasMany('comment', { inverse: 'commentable' });
});
// app/models/comment.js
import DS from 'ember-data';
import { Model } from 'ember-pouch';
export default Model.extend({
DS.belongsTo('commentable', { polymorphic: true });
DS.hasMany('comment', { inverse: 'commentable' });
});
The Problem
Calling post.get('comments') loads nothing. If comments are loaded into the store separately, however, then post is able to correctly render comments:
// In the console (being mindful that `post.get('comments')` returns a promise)
const post = store.findRecord('post', '123');
post.get('comments').get('length'); // => 0
store.findAll('comment');
post.get('comments').get('length'); // => 12
What worked for me during an experiment (although I was heavily modifying ember-pouch within the adapter) was using post.get('comments').content.length but don't ask me why this is so and if it is supposed to be that way ...
EDIT:
It seems the problem is that the data is not loaded at that time. So probably something like post.get('comments').then(function() {this.debug(post.get('comments').length})) will work.

str.replace is not a function in ember 2.1.0

Upgrading Ember project from
Ember.js 1.13.7
Ember-data 1.13.7
to
Ember.js 2.1.0
Ember-data 2.1.0
Getting Following Error
TypeError: str.replace is not a function
model (trends.js)
import DS from 'ember-data';
export default DS.Model.extend({
name:DS.attr('string',{defaultValue:''}),
daterangekey:DS.attr('number',{defaultValue:0}),
daterange:DS.attr(),
actiondata:DS.attr(),
criteria:DS.attr('string',{defaultValue:function(){
return [];
}}),
unit:DS.attr('number',{defaultValue:1}),
viewtype:DS.attr('number',{defaultValue:3})
});
Route (trends.js)
import Ember from 'ember';
export default Ember.Route.extend({
model:function()
{
return this.store.findAll('trend');
},
});
payload returning form server is
{"trends":[{"viewtype":3,"name":"Trend A","daterangekey":0,"type":1,"unit":1,"actiondata":[{"label":"action 2","id":3}],"criteria":[],"id":"1000000000027"}]}
I'm really trying to wrap my head around EmberJS but I've been stuck here on this for the last two days!
Thanks
Edited
model (trends.js)
import DS from 'ember-data';
export default DS.Model.extend({
name:DS.attr('string',{defaultValue:''}),
daterangekey:DS.attr('number',{defaultValue:0}),
daterange:DS.attr(),
actiondata:DS.attr(),
criteria:DS.attr({defaultValue:function(){
return [];
}}),
unit:DS.attr('number',{defaultValue:1}),
viewtype:DS.attr('number',{defaultValue:3})
});
Just remove "type":1 from the payload. Its bug in ember-data and it has been fixed in 1.13.12 release.
https://github.com/emberjs/data/pull/3725
criteria is an array but you define it as string.
Change like this :
criteria:DS.attr({defaultValue:function(){
return [];
}}),

Fixture file not found in Ember project

Is there a basic tutorial or guide on using Ember fixtures? I have gone through the tilde training but it drops right in the middle of a project and I am trying to start from Ember new following the same conventions taught in the course.
I have set up the following routes and fixture:
// routes/application.js
import Ember from 'ember';
import speakers from 'models/speaker-fixtures';
export default Ember.Route.extend({
model: function() {
return speakers;
});
// fixture app/models/speaker-fixtures.js
export default [{
id: "1",
twitterHandle: "foogirl",
name: "foo girl",
avatar: ""
}, {
id: "2",
twitterHandle: "fooboy",
name: "foo boy",
avatar: ""
}];
// adapter/application.js
import DS from 'ember-data';
export default DS.FixtureAdapter.extend({});
// serializer/application.js
import DS from "ember-data";
export default DS.RESTSerializer.extend({});
<.code>
error received :
File: project-voice/routes/application.js
ENOENT, no such file or directory '/Users/../tmp/tree_merger-tmp_dest_dir-VUc8t50a.tmp/models/speaker-fixtures.js'
Is there something I am missing that will help ember find my fixture file? This is my first attempt in creating an app outside a tutorial and I am a bit lost. *I also tried setting up the fixture in the model how it explains in the embercli doc and could not get that work.
Any push in the right direction would help tremendously. Thanks
The path indeed needs to be relative.
import '../models/speaker-fixtures';

Cannot read property 'determineRelationshipType' when trying to save() Model with hasMany Relationship

I'm running into a tough bug when trying to save a record with the LocalStorage Adapter that has a hasMany relationship (Using Ember CLI). What I'm trying to do is save a product to a bag when a user clicks on a "Add to Bag" button. I'm getting this error in my console:
Uncaught TypeError: Cannot read property 'determineRelationshipType' of undefined
Product Model:
import DS from 'ember-data';
export default DS.Model.extend({
...
bag: DS.belongsTo('bag')
});
Bag Model:
import DS from 'ember-data';
export default DS.Model.extend({
products: DS.hasMany('product', {async: true})
});
Here's the action in the controller:
import Ember from "ember";
export default Ember.ArrayController.extend({
actions: {
addToBag: function(model) {
var bag = this.store.createRecord('bag');
bag.get('products').then(function(products) {
products.pushObject(model);
bag.save();
});
}
}
});
Would anyone have an idea as to what's going wrong? Or another way to approach this? Seems like a similar issue was reported here. Would greatly appreciate any help! Thank you in advance.
I started a project using the emberfire adapter and ran into the same issue.
Without going to deep into this, it looks like ember-data beta.10 deprecated a feature that was necessary for hasMany to work. (Further reading https://github.com/firebase/emberfire/issues/123)
Downgrading to ember-data beta.8 fixed the issue for me.
This is necessary until the adapters (emberfire and/or localstorage) can be updated.
In my ember-cli project I did:
rm -rf vendor/ember-data/
bower cache clean ember-data
Edit files vendor/emberfire/bower.json and vendor/emberfire/.bower.json to say "ember-data": "1.0.0-beta.8".
bower install
I re-investigated this issue and looks like it was addressed in a recent update to ember-localstorage-adapter. Specifically, the reference to DS.RelationshipChange was removed.
In my bower.json, I defined my ember-data version back to 1.0.0-beta.11 and also defined my ember-localstorage-adapter version to the latest version, 0.5.0. Here's the relevant info in the bower.json file:
{
"name": "****",
"dependencies": {
"ember": "1.8.1",
"ember-data": "1.0.0-beta.11",
"ember-localstorage-adapter": "~0.5.0",
}
}
This error is no longer appearing!

Ember Cli Inflector adjustments

Where/how can i adjust the Ember.Inflector Class / create an instance of it that ember-cli picks up?
Thanks!
I generated an initializer and put this data there. This ensures it loads before anything that might need it. Like the model, adapter, or serializer.
initializers/inflector.js
import Ember from 'ember';
export function initialize(/* container, application */) {
var inflector = Ember.Inflector.inflector;
inflector.uncountable('aamc-pcrs');
}
export default {
name: 'inflector',
initialize: initialize
};
I placed it in the model file and it worked fine:
import DS from 'ember-data';
import Ember from 'ember';
var inflector = Ember.Inflector.inflector;
inflector.irregular('nota', 'notas');
inflector.singular(/nota/, 'nota');
export default DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
language: DS.attr('string'),
body: DS.attr('string')
});
The Ember Guides covers this in Models - Customizing Adapters:
Create the file app/models/custom-inflector-rules.js:
import Inflector from 'ember-inflector';
const inflector = Inflector.inflector;
inflector.irregular('formula', 'formulae');
inflector.uncountable('advice');
// Meet Ember Inspector's expectation of an export
export default {};
Then in app/app.js add the line:
import './models/custom-inflector-rules';
And if you want to use this in a unit test for a serializer/adapter then you can just import the custom-inflector-rules file into the test.