When running this code in my titanium app:
var events = Alloy.createCollection('Events');
events.fetch({ async : false });
var firstEvent = events.first();
var eventId = firstEvent.eventId;
Ti.API.info(JSON.stringify(firstEvent));
Ti.API.info(eventId);
I get this output:
[INFO] {"eventId":"62243","eventDate":"2014-10-27T12:46:09.364-04:00","eventName":"Atlanta, GA","street1":"street address","city":"Atlanta","state":"GA","zip":"30303"}
[INFO] <null>
Why is the eventId variable null? How do I get the eventId, or any of the other object properties, properly?
Thanks.
events.first()
returns a backbone model
you can do
events.first().get("eventId")
to get the eventId
that's how we get model properties properly
Related
Got a server returning a JSON object like so:
{
'key1':'value'
'key2':{
'key2_0':'value'
}
}
And a collection:
var Collection = Backbone.Collection.extend({
url:api.url//which returns the object above
});
var collection = new Collection();
collection.fetch({
success:function(data){
//do something
}
});
Now i need to use certain properties of the collection throughout my application, but say i need key1, i always have to do collection.at(0).get('key1');//returns 'value', because the data returned is stored within the collection, in a new Array at key 0.
Question:
How to directly... collection.get('key1')//now returns undefined... because it is.
I know i could expose an object to the global scope in the collection success function some_other_var = data.toJSON()[0] and access the some_other_var properties directly, but that's not what i'm looking for;
In order to use the get() function from a Backbone.Collection you need to know the model id or cid wanted.
For instance, lets say your data coming from the server is like follow:
[{
id: '123',
name: 'Alex'
}, {
id: '456',
name: 'Jhon'
}]
In that case you can do this:
this.collection.get('123').get('name') // Return "Alex"
Keep in mind that collection is just a set of model, so behind the scenes by doing collection.get() you are getting a model
Tip: If you don't have any kind of id in your server data, there is always the option of using underscore methods:
find
filter
some
contains
etc
It seems like you're trying to ascribe attributes to a collection, but a collection is merely a set of models. Having additional data that is constant throughout the collection suggests that it should be wrapped inside another Model, which is demonstrated here: Persisting & loading metadata in a backbone.js collection
In my Backbone App i'm trying to merge collections by using the _.union-method from Lodash (Underscore).
So I have the following:
var myCollection = _.union([carsCollection], [motorcycleCollection], [bikeCollection]);
when I do console.log(collection) it gives me [child, child, child] where each child contains an array of the Models from the collection and its attributes. So far so good, my question is now:
How can I display this in a View? I tried:
this.insertView(new View({collection: myCollection }));
but that didnt work...
Does anyone know whta the issue is here?
Backbone collections are not arrays of models, using _.union on them won't produce a collection of models. You have to work with the collection.models and then build a new collection :
var models = _.union(
carsCollection.models,
motorcycleCollection.models,
bikeCollection.models
);
var unitedCollection = new Backbone.Collection(models);
See http://jsfiddle.net/nikoshr/uc5cn/ for a demo
var manifest = Alloy.Collections.manifest;
function submit(){
list_view.open();
manifest.fetch();
var model = Alloy.createModel('manifest', {
bill: //(need to be an incremented value),
Num_of_elements: $.items.value
});
manifest.add(model);
model.save();
}
so i have this function the when clicked it will save info the the data base, or model not to sure what the differante is yet. i want to be able to get that "Num_of_elements" value from the model at a latter time, how would i go about this. model.execute(sql query)?
If you have the bill number you can just do this, using the where function of backbone collections:
var manifest = Alloy.Collections.manifest;
var manifest = manifest.where({
bill : // your bill number
})[0]; // Get the first one returned
I'm testing Localstorage of Backbone with the following code. After saving the model, it's supposed to create an id and also a cid that is accessible at Model.cid. It's logging the id in the console (see below), but not the cid. Here's a fiddle that recreates the problem http://jsfiddle.net/mjmitche/haE9K/2/
Can anyone see what I'm doing wrong?
// Model Creation with defaults
var Todo = Backbone.Model.extend({
localStorage: new Backbone.LocalStorage("todos"),
defaults: {
title:'new todo...',
description:'missing...',
done:false
}
});
var myTodo = new Todo({});
console.log("Before save id:"+myTodo.get('id')); // Unique Id from Server
console.log("Before save cid:"+myTodo.cid); // Client side id
myTodo.save();
console.log(myTodo.get('title'));
console.log("After save id:"+myTodo.get('id'));
console.log("After save cid:"+myTodo.cid);
Console results
Before save id:undefined app.js:16
Before save cid:c0 app.js:17
new todo... app.js:20
After save id:99bc7f4c-8837-39f4-91e9-90760d8ee8cd app.js:21
After save cid:c0 app.js:22
The cid is created when the model is created and never change then. See the docs:
A special property of models, the cid or client id is a unique
identifier automatically assigned to all models when they're first
created
Lets take a look in the constructing function of Backbone.Model:
var Model = Backbone.Model = function(attributes, options) {
.
.
.
this.cid = _.uniqueId('c');
.
.
.
};
As you can see, the cid is created there using underscores uniqueId method.
I am trying to save Event. But it does not work. Will you please help? thanks alot
query = []
query = Identity.all().filter('name =', 'k').fetch(1)
if query:
for q in query:
event_id = q.key().id()
Event(description=description, identity=event_id)
Event Model
class Event(search.SearchableModel):
description = db.TextProperty(required=True)
identity = db.ReferenceProperty(Identity)
Getting error message >
if value is not None and not value.has_key():
AttributeError: 'long' object has no attribute 'has_key'
Your assigning the id of the object into a ReferenceProeprty, this is wrong.
Your code should look something like this:
query = Identity.all().filter('name =', 'k').get()
if query:
Event(description=description, identity=q)
Also instead of having your own name attribute you use use the buitin key_name attribute that each Model has, its faster and cheaper.
query = Identity.get_by_key_name(k)
if query:
Event(description=description, identity=q)