Access data from a local model - arrays

Here is my problem, I hope some one can help me here, I'm developing a mobile app in qx.
In the application.js I call for a JSON in a server through the qx.store.json(url) that creates a model that I bind to a offline model to access the data offline in the app.
Everything good so far then when I try to access the data in the offline model it doesn't let me. The original JSON data is
array(timestamp=>time(),
userdata=>array(
array(userid=>0...),
array(userid=>1...)))
When I debug the JSON or the offline data with obj.getItem(1) it always returns me qx.data.model.userdata.
I'm trying to use the data inside the array of userdata to validate a user in a foreach statement but qx.data.model.userdata always returns undefined.
I try obj.getUserdata(), obj.getItem(1), obj being the offline model.
What am I doing wrong? It isn't a model a store for data, or it can only be used as binding data to an widget?

If the item at index 1 was an Array, obj.getItem(1) would return an instance of qx.data.Array. Since it returns an instance of qx.data.model.userdata, that means the model item is actually an object with a single property named "userdata" and you would access the value by calling obj.getItem(1).getUserdata().

Related

extjs store.getById() fails

I'm trying to get an Associated Model (E.g. groups and associated users) from a store with:
Ext.each(this.getView().getSelection()[0].getAssociatedData().users,function(element){
var theuser = myStore.getById(element.id);
theuser.set('deactivated','true');
}
This works for the first 25 Users (id 1-25) however the store is filtered through a pagination plugin. In reason of the filtering with offset and limit the requested id isn't in the local store.
any idea on how to force the store to get the model from remote in case the id isn't available in the local cache?
Or is it anyhow possible to use the data from getAssociatedData, change something and write the record back through the writer proxy?
thx, I really appreciate your help!
The store's getById() method will only return the locally-available records.
If you want to retrieve a model, and you know the id already, you can simply do <model class>.load(id). (If the model is unknown, you can do myStore.getModel().load(id)
Note that the load method returns immediately, but it returns a stub - you'll want to use a callback to process the change to the deactivated field.
In ExtJS5, the Session support will help ensure that the models referred to in both the association store and your myStore object refer to the same model instance.

Angular ngResource with AppEngine Cursor

I'm using Google AppEngine as backend and AngularJS as front end for web application I'm making. I'm presenting data in pages to the user.
AppEngine has the ability to select data and return 3 pieces of information: the items selected, indication if there are more items and cursor for the next page.
I need to return all 3 pieces to the client app so it can present the fetched items and allow the user to go to the next page.
I also would like to use ngResource to interact with the server.
The problem is that ngResource expect the list of items to be a list and here it is an object with the 3 pieces.
Is there a way to modify ngResource a bit so that after fetching the data it will use the items to build the array of items?
Not necessarily, ngResource can deal with arrays as well as single item or json object. The standard get operation returns a object whereas query returns array. Bottom line as long it is a valid json data ngResource would work.
You can always call get on the resource, get the data into a json object and then it can have sub-properties which can be of array type.
You can share your specific structure and the community can help you with understand how to access it using ngResource

How to identify a BackboneJS Model serverside

I'm just getting my head around BackboneJS, but one of the (many) things I'm still struggling with is how exactly the Models Sync up and relate to the serverside DB records.
For example, I have a Model "Dvd", now I change an attribute on the "Dvd" Model, the Name for example, and then I call save(), how exactly does the server side know what DB record to update? Does Backbone hold the DB row ID or something else?
Yes, typically you will set up your Backbone models so that they have database row IDs for any objects you're working with. When one of them is brand new on the client side and not yet saved to the server, it will either have a default or no ID, which doesn't matter since the server will be assigning the ID if and when the initial save transaction succeeds.
Upon saving or updating a model item, Backbone expects the server to reply with some JSON that includes any attributes that have changed since the save or update request was made. In the response to the initial save request, the server informs the client of a newly saved item's row ID (and you can also send along any other information you might need to pass to the client at the same time).
By default, the 'id' attribute of a model object is assumed to be its unique identifier, but backbone lets you change this if you're using a different identifier for the primary key. Just give your model an idAttribute parameter (see the docs for Backbone.Model.extend()) to do that.
Meanwhile, either the urlRoot parameter or a url function can be given to your models to characterize the urls that should be used to send the various ajax requests to the server for a given model.

backbone.js model and collection overhead

When I fetch models or collections from the server, I am not able to access properties of the model unless I stringify then re-parse. Presumably the models themselves have some extra overhead from backbone.js? Note that in the below code I can perform stringify/parse sequentially, which is supposed to give the same result as I started with. However, clearly I have killed off some superfluous info by performing these two steps because my model's properties are now exposed differently from before. Surely I do not need to go through these two steps to access my model properties, right?
Eg.
thismodel = /// assume this came from server fetch
alert(thismodel.name); // DOES NOT WORK - undefined
jsonmodel = JSON.stringify(thismodel);
var providerprefslistJSON = jQuery.parseJSON(jsonmodel);
alert(providerprefslistJSON.name); // WORKS
Backbone Model objects are not plain old JavaScript objects. They keep their attributes in an internal hash. To access the name attribute you can either do this:
alert(thismodel.attributes.name);
Or better yet use the get() method:
alert(thismodel.get("name"));
The reason it works when you convert the model to JSON and then back again is because JSON.stringify calls the toJSON() method, which creates a JSON string from the internal attributes hash, meaning when you parse that string you get a plain old JavaScript object - which is not the same as a Backbone Model object.
First, are you trying to access the property of the model or response?
From alert(thismodel.name) it would seem that you're going for a property of the model not the attribute. If you're looking for the model attribute then perhaps you want alert(this.model.get('name'))
If you're indeed going for model.name, then basically the problem may lie in how you're parsing the data. Say for example the JSON from your server is like this {'name':'Jimmy'}.
While the model.response the raw JSON sent has "Jimmy" namespaced under object.name, Backbone will automatically take that and turn it into a model attribute unless instructed otherwise (e.g. modelObj.attributes.name) at which point you'd use the get() function.
You should be able to access model data fairly simply if everything works.
E.g. Fetch
var model = new MyModel();
model.id = 1;
model.fetch({
success: function(model, response) {
console.log(model.get('name')); // The model name attribute
console.log(response.name); // The RAW response name property
}
});
Or maybe your server isn't sending the data back as JSON data. Is the server response content-type="application/json" ?
Some things to check.

Are collections required?

Sorry this is a noob question but if I only need some initial data when the application first loads is a collection always needed or can the model fetch the data and pass it directly to the view?
Nothing in backbone is really "required". It's a very thin, more-than-one-way-to-do-it framework. Jeremy recommends data that can be bootstrapped in the initial page load be handled that way, so your HTML could include you initial data as JSON in a <script> tag. You can pass that JSON to a Backbone.Collection (if it's a list of similar records) or a new Backbone.Model (if it's a single domain object). You can also just use a model and call model.fetch to get your initial data. Model vs. Collection is more about single domain object with name/value pairs vs list of many objects where iterating, sorting, filtering are common.

Resources