I have a basic order entry form with billing and shipping address fields (both based on an 'address' model). I have a checkbox that says "Billing address same as shipping?" that, when checked, will copy the billing address data to the shipping address.
How would I do this? It's not quite apparent to me. I'm thinking that when the "next" button is clicked, if the "billShipSame" value = true then copy the data. But how do you actually copy the data? Or am I just approaching this problem wrong?
The model looks like:
export default DS.Model.extend(Validations, {
type: attr('string'),
firstName: attr('string'),
lastName: attr('string'),
address1: attr('string'),
address2: attr('string'),
city: attr('string'),
state: attr('string'),
country: attr('string'),
postalCode: attr('string'),
phone: attr('string')
});
And here's a stripped-down version of how I'm using them:
billingAddress: computed(function() {
return this.get('store').createRecord('address', { type: 'billing'});
}),
shippingAddress: computed(function() {
return this.get('store').createRecord('address', { type: 'shipping'});
}),
orderModel: computed(function() {
return this.get('store').createRecord('order', {
billingAddress: this.get('billingAddress'),
shippingAddress: this.get('shippingAddress')
});
}),
I would suggest having you "same as billing" radio button trigger an action that copies the data into the appropriate fields. That way by the time someone clicks next, your data model is in good shape and your submit action can focus on saving
Edit:
These easiest way to copy values between two models is as follows:
shippingAddress.setProperties(billingAddress.getProperties('firstName','lastName')); // etc
Believe that should handle what you're after ...
Related
Ex:
I have model/test.js
export default Model.extend({
name: attr('string'),
email: attr('string'),
address: attr('string'),
age: attr(),
phone: attr()
});
In component.js
list: computed('model.{}', function() { });
Fetching data in route and passing it to the template. In child component i am trying to access it. initially data passed will be like
{
'data': {
name: 'test'
}
}
later sending data as
{
'data': {
name: 'test',
email: 'test#gmail.com',
address: 'XYZ',
age: 10,
phone: 423423
}
}
But in computed property it is not listening second updated data. I want to listen dynamically each property in model. It will work if i give like
list: computed('model.{name,email,address,age,phone}', function() { });
But i want some other solution for it. Dynamically need to listen each property in model object.
If you are dealing single object, then what you have is the right and only possible way to go.
list: computed('model.{name,email,address,age,phone}', function() { });
Suppose if your model is array of object then you can do the below things,
list: computed('model.#each.{name,email,address,age,phone}', function() { });
You can try using #each in the computed property.
list: computed('model.#each', function(){});
It will work for all properties inside the model class. But it works only one level deep.
That is controller chat.js code
queryParams: ['offer_id'],
offer_id: null,
filteredChat: Ember.computed('model.#each.offer_id','offer_id',
function() {
return this.get('model').filterBy("offer_id" ,this.get("offer_id")).filterBy("id", this.get("offer_id"))
}),
I am filtering the chat with offer_id.
I want to know that can i use the filterBy like this two times
and that is my route chat.js code
queryParams:{
offer_id:{
refreshModel : true
}
},
model(params) {
return this.store.query("chat", params).then(() => {
let model = this.store.peekAll("chat")
return model
})
},
My Model chat.js
message: attr('string'),
offer_id: attr('string'),
stamp: attr('string'),
type: attr('string'),
You can do this, but be careful, your dependency string is wrong. You should include model.#each.id as well.
Also you need to understand that this will result in an array with the items that both the offer_id and the id are the same and exactly the offer_id you'r filtering on. This will not result in the items that either the offer_id or the id are equivalent to you filter offer_id.
If you want the or version you could do this:
return [
...this.get('model').filterBy("offer_id" ,this.get("offer_id")),
...this.get('model').filterBy("id" ,this.get("offer_id")),
]
so you see, if this works depends absolutely on what you expect.
I'm writing a custom data adapter (doesn't talk to webserver) and I need to know whether hasMany relationship was changed or not to persist relationship data.
App.Note = DS.Model.extend({
content: attr('string'),
createdAt: attr('date'),
tags: DS.hasMany('tag', { async: true })
});
App.Tag = DS.Model.extend({
name: DS.attr('string'),
notes: DS.hasMany('note')
});
I need to implement adapter's updateRecord:
updateRecord: function(store, type, record) {
// record.changedAttributes() returns changed attributes but not relationships
}
In my case list of tags attached to note may change (via note.get('tags').addObject(..) / removeObject(..)) so I need to get a diff. What's the best way to do that? I can remove all tags in a database and insert new tags but that's not efficient at all
I have the following models:
App.Company = DS.Model.extend({
name: DS.attr('string'),
accounts: DS.hasMany('App.Account', {
inverse: 'company'
})
});
App.Account = DS.Model.extend({
login: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
email: DS.attr('string'),
password: DS.attr('string'),
password_confirmation: DS.attr('string'),
company: DS.belongsTo('App.Company')
});
The company is defined as being embedded in the account:
DS.RESTAdapter.map('App.Account', {
company: { embedded: 'always' }
});
When I create a new account, the company data is correctly embedded in the account data and I'm seeing the POST request that I expect on the server side:
Started POST "/accounts" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by AdminUsersController#create as JSON
Parameters: {"account"=>{"login"=>"fsdfdf", "first_name"=>"fgdfgh", "last_name"=>"fsfdsfdsfsd#fgfdgdfgf.de", "email"=>"dsfdsgds#frgdfgfgdfg.de", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "company"=>{"name"=>"gfdhgtrhzrh"}}}
However, I'm also seeing an additional POST request for the company itself:
Started POST "/companies" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by CompaniesController#create as JSON
Parameters: {"company"=>{"name"=>"gfdhgtrhzrh"}}
I'm setting up the models as follows:
this.transaction = this.get('store').transaction();
var account = this.transaction.createRecord(App.Account, {});
account.set('company', this.transaction.createRecord(App.Company, {}));
When the user clicks save, I simply commit the transaction:
this.transaction.commit();
Is that a bug or am I doing something wrong? Spent quite some time on that already...
Thanks for help!
this.transaction.createRecord(App.Company, {})
The code fragment creates the separate company entity. Is it really such a surprise there is a post action for it?
As far as I remember that was never actually supported in the (old) version of Ember Data I used back then. Newer versions handle that case differently anyway so I'd say this is outdated and close it.
My model is defined as follow:
App.User = DS.Model.extend({
primaryKey: 'username',
username: DS.attr('string'),
name: DS.attr('string')
});
My custom Adapter map:
DS.SocketAdapter.map('App.User', {
primaryKey: 'username',
username: DS.attr('string'),
});
I am testing this model out by typing on console:
App.User.createRecord({username:"user_1"});
var r = App.User.find("user_1");
console.log( r.serialize() );
>> Object {username: null, name: null ..... all null}
But it retuns a "null" Object. Also tested:
App.User.find({username:"user_1"});
But this is doing a remote request. I read that Ember Data does allow you to find records via attributes other than the ID.
So what I am doing wrong in telling Ember data my custom primaryKey?
I think your problem lies in the fact that you are defining username twice. if you map username from your json to your model's primaryKey trough your Adapter, then you should avoid to do the same on the model I guess. There are different approaches where to define the mapping, but the Adapter is the most appropriate place In your case, see here for more details: https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md#mapping
change your code like so and it should work:
// Model
App.User = DS.Model.extend({
name: DS.attr('string')
});
// Adapter
DS.SocketAdapter.map('App.User', {
primaryKey: 'username'
});
now try to create a new record
App.User.createRecord({username:"user_1", name:"foo"});
and then find the record by it's id as you already did:
var r = App.User.find("user_1");
this
console.log( r.serialize() );
should then give you at least:
>> Object {name: "foo" ...}
hope it helps