How to commit related models - ember.js

I'm working with ember-pre4 and ember-data rev 11.
Models:
A = DS.Model.extend({
//some other fields
b: DS.hasMany('B')
})
B = DS.Model.extend({
//some other fields
a: DS.hasOne('A')
})
In my router I create an instance of model A and create an instance of model B and connect them. They both don't have server-side id. Something like this:
var a = A.createRecord();
b = B.createRecord();
b.set('a', a)
a.get('b').addObject(b)
When I want to save those models I make:
a.transaction.commit()
And I expected to see:
Save a with empty b // []
After saving a pass a's id into b and save b
After saving b refetch a
But unfortunately ember does 2 request in parallel and a's request data is:
"{//some fields, b: [/url_to_b//]}" // we dont have b's id
b's request data is:
"{//some fields } // we dont have a's id
What is the best way to solve this problem, does new ember have a default solution for my situation or I should do all stuff manually?

Solution:
I wrapped createRecord function in waitForParents function:
waitForParents:function (record, callback, context) {
var observers = new Em.Set();
record.eachRelationship(function (name, meta) {
var relationship = get(record, name);
if (meta.kind === 'belongsTo' && relationship && get(relationship, 'isNew')) {
var observer = function () {
relationship.removeObserver('id', context, observer);
observers.remove(name);
finish();
};
relationship.addObserver('id', context, observer);
observers.add(name);
}
});
finish();
function finish() {
if (observers.length === 0) {
callback.call(context);
}
}
},
createRecord:function (store, type, record) {
this.waitForParents(record, function () {
// createRecord code here
}
}

Related

Adonis.js - Seeding Users and Profiles throwing DB error (Postgres)

I am trying to write a seed file for users and profiles with a one to one relationship and currently getting an "error: relation 'user_profiles' does not exist". From digging around, it seems like Adonis will assume this as a pivot table in the case of a many to many relationship. What I (think or intend to) have is a one to one relationship between users and profiles. Thanks in advance! Newbie to SQL and Adonis. As a side note, the user persists to the db, but there is no corresponding profile.
// My User Schema
class UserSchema extends Schema {
up () {
this.create('users', (table) => {
table.increments('id')
table.string('username', 80).notNullable().unique()
table.string('email', 254).notNullable().unique()
table.string('password', 60).notNullable()
// table.integer('profile_id').unsigned().references('id').inTable('userprofiles')
table.timestamps()
})
}
down () {
this.drop('users')
}
}
// My Profile Schema
class UserprofileSchema extends Schema {
up () {
this.create('userprofiles', (table) => {
table.increments()
table.string('first_name')
table.string('last_name')
// table.integer('user_id')
// .unsigned().references('id').inTable('users')
table.integer('user_id')
.unsigned()
.index('user_id')
table.foreign('user_id')
.references('users.id')
table.timestamps()
})
}
down () {
this.drop('userprofiles')
}
}
My User model includes the following relationship definition:
profile () {
return this.hasOne('App/Models/UserProfile')
}
// Seed script
class UserSeeder {
async run () {
try {
const user = await Factory.model('App/Models/User').create()
const userProfile = await Factory.model('App/Models/UserProfile').make()
userProfile.user_id = user.id
await user.profile().save(userProfile)
} catch (e) {
console.log('Error From Seeder: ', e);
}
}
}
Error code '42P01' and can post whole body if needed. Thanks!
On your Model userProfile, set table name as follows.
class User extends Model {
static get table () {
return 'userprofiles'
}
}

Loopback get filtered result in the same order

I have two loopback model like
Model1
{
"id":"",
"name":"",
"model2Ids":[]
}
This model has an array of model 2 ids using the referencesMany relation.
I want to query Model from the remote method of model 1.
My remote method is as follows
Model1.customRemoteMethod = function(id, cb) {
var Model2 = Model1.app.models.model2;
Model1.findById(id, function(model1Error, model1Obj) {
if (model1Error) cb(model1Error);
if (model1Error.model2Ids) {
var model2Filter = {
'where': {
'id': {'inq': model1Obj.model2Ids}
}
};
Model1.find(model2Filter, function(model2Error, model2s) {
if (model2Error) cb(model2Error);
cb(null, Object.assign({}, JSON.parse(JSON.stringify((channelMapObj))), {
model2s: model2s
}));
});
}
})
})
I want the models that are returned(that is the filtered results) to be in the same array as that of my ids in Model1.Is there a way to do it.?
For example if I query using id 2,1,3.
Then the results should be the same order and not 1,2,3(as found in model 2).
The only solution that I have would be to loop over the id, and the use findById to get all results in same order.

Ember - Within action, result is defined, returnvalue of same action logged in parent action is undefined? Why?

Quick and shortly I have following problem:
I have following two actions within a component in Ember:
createData: function(user) {
let collection = [];
for (let i = 0; i < user.posts.length; i++) {
let data = this.send('createSingleData',user.posts[i], user, 'post');
console.log(data);
collection.push(data);
}
return collection;
},
createSingleData: function(data, user, type) {
let entitySkeleton = {
name: data.place.name,
belongsTo: user.id,
position: {
data.place.location.longitude,
data.place.location.latitude
}
};
console.log(entitySkeleton);
return entitySkeleton;
}
the first log - within createSingleData, right before returning the logged value - writes the entitySkeleton as Object into the console - as expected.
However, the console.log(data) - within createData - writes 'undefined' to the console.
Is there any aspect of asynchrounosity I didn't respect?
P.S.:
I also logged any paramater within createSingleData, they are all set properly.
The variable collection also only gets pushed 'undefined'.
You cannot return the value from action, instead you can set property from the action.
how to return values from actions in emberjs
actions: {
PrintSomething: function() {
let obj = [{a: 'raj'}, {a: 'Prudvi'}, {a : 'thimappa'}]
console.log('before', obj);
this.send('returnSomething', obj);
console.log('after calling action', this.get('returnvalue'));
},
returnSomething: function(obj) {
obj.push({a: 'FSDFSDF'})
var data = obj;
this.set('returnvalue', data);
}
}

Add payload in Ember deleteRecord

I have a requirement to include remarks from user in the payload whenever he tries to delete an item. So far, I have this:
let remarks = this.get('remarks');
let id = this.get('itemID');
this.store.findRecord('item', id).then(function (selectedItem) {
// TODO - DELETE doesn't accept payload in body?
selectedItem.destroyRecord({remarks:remarks}).then(function(response){
Ember.debug('delete successful:'+JSON.stringify(response));
Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');
context.set('successful', true);
context.set('message', context.get('i18n').t('success.role.delete'));
}).catch(function(error){
Ember.debug('delete failed:'+JSON.stringify(error));
Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');
context.send('showErrors', error);
});
});
It doesn't work. So does setting the remarks value in the model like:
...
this.store.findRecord('item', id).then(function (selectedItem) {
selectedItem.set('remarks', remarks);
selectedItem.destroyRecord().then(function(response){
...
I am trying to override the deleteRecord but I don't know where to start or how to do it.
Anyone have ideas? Thanks!
You can easily achieve this kind of behaviour by extending your application adapter with the following mixin:
/* app/mixins/delete-with-playload.js */
import Ember from 'ember';
export default Ember.Mixin.create({
deleteRecord(store, type, snapshot) {
var id = snapshot.id;
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, snapshot);
return this.ajax(this.buildURL(type.modelName, id, snapshot, 'deleteRecord'), "DELETE", {
data
});
}
});
Then just add it to your application adapter
/* app/adapters/application.js */
import RestAdapter from 'ember-data/adapters/rest';
import DeleteWithPayloadMixin from '../mixins/delete-with-payload';
export default RestAdapter.extend(DeleteWithPayloadMixin);
This will result a payload identical to the payload of PUT method, meaning a payload of the form:
{
"<model-name>": {
// model's serialized attributes
}
}
Now all you have to do is to set the desired attributes on the record before deleting, and destroy the record.
model.setProperties({
deleteReason: 'whatever'
});
model.destroyRecord();
/*
results a DELETE request when requestBody is "{
"<model-name>": {
...
"deleteReason": "whatever"
...
}
}"
*/

How can I clone an Ember Data record, including relationships?

I've figured out that I can clone an Ember Data record and copy its Attributes, but none of the belongsTo/hasMany relationships are cloned. Can I do this somehow if I don't know what relationships would be possible, going off of the relationships that exist?
For reference, here is what I've got that will clone an Ember Data record's attributes:
var attributeKeys = oldModel.get('constructor.attributes.keys.list');
var newRecord = this.get('store').createRecord(oldModel.constructor.typeKey);
newRecord.setProperties(oldModel.getProperties(attributeKeys));
A few improvements to Daniel's answer that allows providing overrides, and clears the id for new records, to be safe. Also I prefer not to call save within the clone method, but leave it up to the caller.
DS.Model.reopen({
clone: function(overrides) {
var model = this,
attrs = model.toJSON(),
class_type = model.constructor;
var root = Ember.String.decamelize(class_type.toString().split('.')[1]);
/*
* Need to replace the belongsTo association ( id ) with the
* actual model instance.
*
* For example if belongsTo association is project, the
* json value for project will be: ( project: "project_id_1" )
* and this needs to be converted to ( project: [projectInstance] )
*/
this.eachRelationship(function(key, relationship) {
if (relationship.kind == 'belongsTo') {
attrs[key] = model.get(key);
}
});
/*
* Need to dissociate the new record from the old.
*/
delete attrs.id;
/*
* Apply overrides if provided.
*/
if (Ember.typeOf(overrides) === 'object') {
Ember.setProperties(attrs, overrides);
}
return this.store.createRecord(root, attrs);
}
});
Here is a clone function that I use. Takes care of the belongs to associations.
DS.Model.reopen({
clone: function() {
var model = this,
attrs = model.toJSON(),
class_type = model.constructor;
var root = Ember.String.decamelize(class_type.toString().split('.')[1]);
/**
* Need to replace the belongsTo association ( id ) with the
* actual model instance.
*
* For example if belongsTo association is project, the
* json value for project will be: ( project: "project_id_1" )
* and this needs to be converted to ( project: [projectInstance] )
*
*/
this.eachRelationship(function(key, relationship){
if (relationship.kind == 'belongsTo') {
attrs[key] = model.get(key)
}
})
return this.store.createRecord(root, attrs).save();
}
})
There's an addon called ember-cli-copyable that according to its description:
Deeply copies your records including their relations. The mixin is smart enough to resolve not loaded relations and is configurable to what should be shallow/deeply copied or excluded entirely.
Here is the simple way to clone your Ember Model with relationships.
working fine.
Create a Copyable mixin like,
import Ember from 'ember';
export default Ember.Mixin.create(Ember.Copyable, {
copy(deepClone) {
var model = this, attrs = model.toJSON(), class_type = model.constructor;
var root = Ember.String.decamelize(class_type.toString().split(':')[1]);
if(deepClone) {
this.eachRelationship(function(key, relationship){
if (relationship.kind == 'belongsTo') {
attrs[key] = model.get(key).copy(true);
} else if(relationship.kind == 'hasMany' && Ember.isArray(attrs[key])) {
attrs[key].splice(0);
model.get(key).forEach(function(obj) {
attrs[key].addObject(obj.copy(true));
});
}
});
}
return this.store.createRecord(root, attrs);
}
});
Add the mixin in your model,
Note: If you want to clone your child model then, you need to include the mixin in child model as well
USAGE:
With relationship : YOURMODEL.copy(true)
Without relationship : YOURMODEL.copy()