Passing variable into Backbone.where [duplicate] - javascript

This question already has an answer here:
Trouble referencing variable in Collections.where method within render function
(1 answer)
Closed 7 years ago.
I want to form a collection from a JSON based on the obtained value and Backbone's where seemed like a perfect tool, but it looks like it doesn't accept variables. Is it possible to achieve this sort of functionality using some of Backbone, Lodash or Underscore methods?
### collection instantiated above
App.vent.on 'event', (obtained_value) ->
desired_models = collection.where(attribute: obtained_value)
console.log desired_models
### outputs empty array
>[]
It does work when I pass key: value directly, but I need to form collection dynamically. Maybe I've initially taken a false route and the solution is in the another direction?

I'm assuming your goal is to vary the attribute you're searching for, since if the value is varied, object literals should work fine.
You can do it, just not with an object literal inline. Here's how I would do it in JavaScript:
App.vent.on('event', function (obtainedValue) {
var finder = {};
finder[attribute] = obtainedValue;
desiredModels = collection.where(finder);
console.log(desiredModels);
});

that wont work, you need to pass an object so it would be like this
collection.where({attribute: obtained_value});
in coffescript you can do the following
attribute_var=
attribute:obtained_value
collection.where(attribute_var);
best regards

Related

Backbone findWhere finds nothing [duplicate]

This question already has answers here:
How do I get a model from a Backbone.js collection by its id?
(2 answers)
Closed 6 years ago.
My collection has 4494 models. I try to use findWhere to get specific model by id. The findWhere returns 'undefined'.
It works ok if I limit the number of models. The strangeness heppens when the number of models is over 100.
var users = this.usersCollection;
console.log(users);
console.log(users.findWhere({uid: 1}));
While the problem is solved (using findWhere before the collection is actually fetched) thanks to TJ's comment, you might want to use .get instead of findWhere when searching a model by id.
If your User model looks something like this:
var User = Backbone.Model.extend({
idAttribute: 'uid',
// ...
});
You could get a user by id from the collection directly:
var user = users.get(1);
You can also use get with the model's cid or the model instance.
user === users.get(user.cid)
user === users.get(user)
This is better because Backbone keeps a hash of the models with the specified id attribute as the key in addition of the usual array.
The problem was that I tried to use my collection before it was completely fetched.

How to use a bit complex object as a value of a key in localStorage of Google Chrome? [duplicate]

This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 6 years ago.
I'm trying to build a chrome extension that would feed data in the localStorage of the form:
var a = 'YouTube';
var data = {
'title': 'twenty one pilots: Stressed Out [OFFICIAL VIDEO]',
'url': 'https://www.youtube.com/watch?v=pXRviuL6vMY'
};
localStorage.setItem(a, data);
But when I look into the resources in dev. tools, it doesn't show the data object in the value table. How can I make it appear in there?What's wrong with the code?
The Image of the console and the localStorage.
I have tried commands like localStorage.YouTube and localStorage.getItem('YouTube') but it always returns [object Object].
What could be a possible workaround for making something like this possible?
LocalStorage in HTML5 can only store key-values strings. You can't store objects there.
BUT, you can use JSON. To make a string from your object you can use JSON.stringify(yourObj);
and when you want to get value from LocalStorage you simply parse that string and create new object which you can use again. JSON.parse(yourObj);

Underscore JS, Grouping by an array Attribute.

I'm trying to use Javascript to group an array by one of it's attributes.
Essentially I have a list of e-mail templates, and I'm trying to group them by the category that they are in so that I can make a collapsible accordion with it later.
I think I might have the syntax of underscore JS wrong, or I'm addressing my array incorrectly. Currently I am calling groupby using the following command:
console.log(_.groupBy(result, 'im_category'));
but my array looks like the 'im_category' property is hidden under the attributes function. I'm not sure how to get there.
I've attached my console.log of what the array looks like and what happens when I run that command. (I get three different objects when I should get 2 if it's working correctly. )
Your im_category is a property of the attributes object in your businessEntity - _.groupBy is looking for properties of businessEntity. You need to create a function as iteratee:
var grouped = _.groupBy(result, function (item) {
return item.attributes.im_category;
});
http://jsfiddle.net/jwnzh8w0/

How can I build a querystring from an object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
querystring encoding of a javascript object
I'm trying to make a request to a website and embed the result as an iframe in the page using javascript. I need to pass a number of parameters in the request as querystring variables and I'd like to be able to specify the parameters as an object, passing them to a function to produce a querystring so they are easy to read, maintain and manipulate.
How can I construct the querystring of a URL from a JSON object with simple values? I would expect this example:
{
h:300,
w:300,
skip:500,
count:50
}
to produce the following querystring:
h=300&w=300&skip=500&count=50
Are there any existing library functions to do this, or is the best thing to loop over the properties myself?
jQuery has a built-in method for this: jQuery.param()
I recommend you use jQuery's param(). Here is the documentation http://api.jquery.com/jQuery.param/
You would pass your object like so:
var myObj = {
h:300,
w:300,
skip:500,
count:50
}
console.log('=>', $.param(myObj) ); // prints => h=300&w=300&skip=500&count=50

How to dynamically create / destroy JS variables and append / remove their values in a string

I want to create javascript variables on the fly and append their values into a string. I actually am unable to find a method which dynamically declares variables in javascript. Once that is done I would want to do the reverse of this. So my question has two parts : one is related to creating and deleting variables on the fly as per requirement and the other is related to appending or deleting those variables value into a string at a specific point in the string.
There is the eval function.
But! you should ask yourself twice why do you need to "create variables on the fly"
eval on MDN
If I understand what you're after correctly, you should use an object for this:
var o = {};
o.name = "value"; // Creating a property
delete o.name; // Deleting a property
It sounds like you have a specific problem, could you perhaps share a bit more information about your problem?

Categories

Resources