I have a basic question.
I have an item model:
App.Item = DS.Model.extend({
...
user: belongsTo('user')
});
But if I send the JSON:
"user" : ""
it assigns it to the User with the ID of 0. Probably because of Javascript problems to differentiate between an empty string and 0
Does anybody know how to test for an unassigned relationship in a computed property?
Thx!
I'm not seeing the same behavior, would you mind setting up a jsbin?
I'm seeing it attempt to find a record with an empty string. The proper way to do it is to return null.
App.Order.FIXTURES = [
{
id: 1,
cow: "",
name: "Some Order"
}, {
id: 2,
cow: null, // This is the right way to say no element
name: "Some Other Order"
},{
id: 3,
cow: 0,
name: "Some Other Order 3"
}
];
http://emberjs.jsbin.com/AvOYIwE/3/edit
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Currently my data looks like
accountData = [
{accountNumber: 123, users: [{id: 1, email: 'xyz#aol.com', name: 'bob'},{id: 2, email: 'dfg#gmail.com', name: 'tomas'}],
{accountNumber: 345, users: [{id: 3, email: 'abc#yahoo.com', name: 'mark'},{id: 4, email: 'dfg#gmail.com', name: 'peter'}, {id: 5, email: 'gdfgfgd#gmail.com', name: 'tom'}]
]
I have a search bar and I want to be able to search for 'tom' for example. If the user types 'tom' then both accounts should show up. However, if the user types 'tomas' then only accountNumber 123 should show up. Im not exactly sure how to filter these out with regex. My accountData has much more data in it so filtering it this way would be ideal.
I would need it to filter through anything in the array including accountNumber, id, email, and name.
function search(searchTerm: string) {
return accountData.filter(account =>
account.users.some(user =>
user.name.toLowerCase().includes(
searchTerm.toLowerCase()
)
)
);
}
EDIT: Updated in response to comments
Use the Array.map function to filter out the parts of accountData that you do not want.
First step in the map function is to filter out users that match the regular expression
If there are no user matches, check for the other matches on accountData
If there are no matches on account data, return null
Finally, before printing using console.log, filter out any null results using .filter(Boolean)
In the example below mapByExpression creates a map function based on a RegExp that will filter the JSON representation as suggested by #Gavin. It isn't the most efficient way to achieve this, but it is simple.
Note that you should escaping RegExp characters when doing this. I've included an example which calls escapeRegExp.
const accountData = [
{accountNumber: 123, users: [{id: 1, email: 'xyz#aol.com', name: 'bob'},{id: 2, email: 'dfg#gmail.com', name: 'tomas'}]},
{accountNumber: 345, users: [{id: 3, email: 'abc#yahoo.com', name: 'mark'}, {id: 4, email: 'dfg#gmail.com', name: 'peter'}, {id: 5, email: 'gdfgfgd#gmail.com', name: 'tom'}]}
];
function mapByExpression(expression) {
return function(accountData) {
let { users, ...otherFields } = accountData
users = users.filter(user => expression.test(JSON.stringify(user)));
if (users.length) {
return {
...otherFields,
users,
};
} else if (expression.test(JSON.stringify(otherFields))) {
return accountData;
}
return null;
}
}
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
console.log(accountData.map(mapByExpression(new RegExp(escapeRegExp('tom')))).filter(Boolean));
console.log(accountData.map(mapByExpression(new RegExp(escapeRegExp('tomas')))).filter(Boolean));
console.log(accountData.map(mapByExpression(new RegExp(escapeRegExp('123')))).filter(Boolean));
No need for Regex, a bit of Array methods will do:
const accountData = [
{ accountNumber: 123, users: [{ id: 1, email: 'xyz#aol.com', name: 'bob' }, { id: 2, email: 'dfg#gmail.com', name: 'tomas' }] },
{ accountNumber: 345, users: [{ id: 3, email: 'abc#yahoo.com', name: 'mark' }, { id: 4, email: 'dfg#gmail.com', name: 'peter' }, { id: 5, email: 'gdfgfgd#gmail.com', name: 'tom' }] }
]
const searchFilter = "tom";
const filteredData4 = accountData
.filter(acc => acc.users.some(user => Object.values(user).some(val => val.toString().includes(searchFilter))));
I have table with items which each contain a string set, as well as other information. Can I query the table and get back a flag if the set contains a particular entry. I don't want to filter by the set, just find out if it contains an item without pulling back the full set.
For example, say I have the following items:
[
{
things: { "a", "b" },
name: "Coffee"
},
{
things: { "b" },
name: "Tea"
}
]
I would like to query this table with something like Projection: "flag=(thing CONTAINS 'a'), name" and get back:
[
{
flag: true,
name: "Coffee"
},
{
flag: false,
name: "Tea"
}
]
Is this possible?
I have a model with the following list of products...
{
products: [
{
id: 1,
something: "value",
properties: {
name: "Name1"
}
},
{
id: 2,
something: "value",
properties: {
name: "Name3"
}
},
{
id: 3,
something: "value",
properties: {
name: "Name2"
}
}
]
}
If I were to sort on these products by sorting on the "product.properties.name" how would I do that? My first idea was to use the sortableMixin and using the following snippet:
sortProperties: ['properties.name'],
sortAscending: true
But that doesn't really product the result I intended. How should I go about this?
SortableMixin is definitely the way to go. Your controller has to extend the mixin and then you can access the arrangedContent like here: http://emberjs.jsbin.com/gagoyeseni/1/
My backend replies to find all requests:
User.find();
Like this
{ 'users' : [ user1_obj, user2_obj ] }
Ember-data is happy about it. Now if I do a simple single object find:
User.find('user1');
I have tried configuring the backend to return any of the following:
user1
{ 'user1' : user1_obj }
{ 'user' : { 'user1' : user1_obj } }
{ 'user' : user1_obj }
But none of those are working. What should I return from the backend in reply to find("obj-id") requests? According to the documentation about JSON ROOT, the right format looks like:
{ 'user' : user1_obj }
Ember does not complain about it, but the Ember Objects processed have a very strange structure, like this:
As you can see, _reference.record is referring to the top record. Also (not shown here) _data field is empty.
What could be causing that strange nesting?
EDIT
As linked by mavilein in his answer, the JSON API suggests using a different format for singular resources:
{ 'users' : [user1_obj] }
That means, the same format as for plural resources. Not sure if Ember will swallow that, I'll check now.
Following this specification, i would suspect the following:
{
'users' : [{
"id": "1",
"name" : "John Doe"
},{
"id": "2",
"name" : "Jane Doe"
}]
}
For singular resources the specification says:
Singular resources are represented as JSON objects. However, they are
still wrapped inside an array:
{
'users' : [{
"id": "1",
"name" : "John Doe"
}]
}
Using User.find() will expect the rootKey pluralized and in your content an array of elements, the response format is the following json:
{
users: [
{ id: 1, name: 'Kris' },
{ id: 2, name: 'Luke' },
{ id: 3, name: 'Formerly Alex' }
]
}
And with User.find(1) the rootKey in singular, and just one object:
{
user: {
id: 1, name: 'Kris'
}
}
Here a demo showing this working
I'm trying to get to my models data from a view didInsertElement hook, because I need to trigger some actions on the DOM when the template rendering is completed.
with models like:
App.MyParent = DS.Model.extend({
title: DS.attr('string'),
childs: DS.hasMany('App.MyChild')
});
App.MyChild = DS.Model.extend({
name: DS.attr('string'),
parent: DS.belongsTo('App.MyParent')
});
and objects like:
App.MyParent.FIXTURES = [
{ id: 0, title: "some title", childs: [0,2,3] }
]
App.MyChild.FIXTURES = [
{ id: 0, name: "some name", parent: 0 },
{ id: 2, name: "other name", parent: 0 },
{ id: 3, name: "diff name", parent: 0 }
]
Inside the hook function I am getting to the childs property like this:
var childs = this.get("context.content.childs");
then I get an enumerable with the right number of childs, but the child all have undefined properties.
update
Seems like the only place where I can get to a child with App.MyChild.find(someId) is in the browser console, in my app code I only get objects with undefined properties.
I'm puzzled!
To access the childs property, try something like this:
var childs = this.get("context.content.childs");
Also, the fixture data for associations should not use _id or _ids. So use childs and parent instead:
App.MyParent.FIXTURES = [
{ id: 0, title: "some title", childs: [0,2,3] },
];
App.MyChild.FIXTURES = [
{ id: 0, name: "some name", parent: 0 },
{ id: 2, name: "other name", parent: 0 },
{ id: 3, name: "diff name", parent: 0 },
];
http://jsbin.com/ucanam/316/