Sencha Touch - Accessing Associated-Model Store JSON via Nested Looping - json

I've been lurking on Stack Overflow for quite some time now, and have found quite a number of very helpful answers. Many thanks to the community! I hope to be able to contribute my own helpful answers before too long.
In the meantime, I have another issue I can't figure out. I am using Sencha Touch to create a Web-based phone app and I'm having trouble using a nested loop to iterate through some JSON. I can grab the first level of data, but not the items nested within that first level. There is a somewhat related ExtJS thread, but I decided to create my own since ExtJS and Touch diverge in subtle yet important ways. Anyway, here is some code to show where I am:
JSON (truncated - the JSON is PHP/MYSQL-generated, and there are currently actually three sub levels with "title", all of which I can access. It's the sub level "items" through which I can't iterate):
{
"lists": [
{
"title": "Groceries",
"id": "1",
"items": [
{
"text": "contact solution - COUPON",
"listId": "1",
"id": "4",
"leaf": "true"
},
{
"text": "Falafel (bulk)",
"listId": "1",
"id": "161",
"leaf": "true"
},
{
"text": "brita filters",
"listId": "1",
"id": "166",
"leaf": "true"
}
]
}
]
}
Store:
var storeItms = new Ext.data.Store({
model: 'Lists',
proxy: {
type: 'ajax',
method: 'post',
url : LIST_SRC,
extraParams: {action: 'gtLstItms'},
reader: {
type: 'json',
root: 'lists'
}
}
});
Working Loop:
storeItms.on('load', function(){
var lstArr = new Array();
storeItms.each(function(i) {
var title = i.data.title;
lstArr.push(i.data.title);
});
console.log(lstArr);
});
Non-working Nested Loop:
storeItms.on('load', function(){
var lstArr = new Array();
storeItms.each(function(i) {
var title = i.data.title;
var id = i.data.id;
title.items.each(function(l) {
lstArr.push(l.data.text);
});
});
console.log(lstArr);
});
The non-working nested loop gives me the error "Cannot call method 'each' of undefined", in reference to 'title.items.each...'
I suspect this is because I've not set title to be a key to set up a key:value pair, so it just sees a list of strings...but I'm kind of at a loss.
I should mention that the store is populated via two Models that have been associated with one another. I know that the Store can access everything because I am able to do nested iterating via an XTemplate.
Any help will be much appreciated and hopefully returned to the community in kind before too long!'
-Eric

Eric, why the loop?
If your models are associated in the same way that the JSON is nested, then you should just be able to set autoLoad:true on the store, sit back and enjoy.
Anyway, on the assumption that you are needing these arrays for some other unrelated reason, the problem is that you are trying .each on
i.data.title.items
Surely you should be iterating through
i.data.items
Also, if the object is a model, you can use .get() instead of the data object:
var title = i.get('title);

Using new sencha touch 2 framework, you can create associations within the models exactly the same way how your json is returned.
Check Sencha Touch 2 Model Document which tells you the various config options on Model.
You may refer to this example of ST2 Nested List .
Hope this helps.

"title" is not a enumerable object, its a string. To iterate a string you'll need to split it to convert it into an array.
Also, instead of using Ext.each try a simple for (var x in obj) {} or for (var xc in obj.prop) {} If that works then the ext.each method should work as well but if ext cannot iterate the object it will just quietly fail.

Related

Knockout get JSON from Yii2 to observableArray

How can I fill observableArray data obtained from yii2. Let's say I have a static json array in knockout application:
var countries = [{"id": 1, "name": "1"}, {"id": 2, "name": "2"}, {"id": 3, "name": "3"}]
When I do this:
var VM = function(){
this.countries = ko.observableArray(countries);};
It's all good. And I can continue to use it for HTML-tag select. But when I try to retrieve data from the server it does not work. Here's how I'm trying to do.
The data from the server:
$d = Countries::find()->all();
$r = Yii::$app->response();
$r->format = Response::FORMAT_JSON;
$r->data = $d;
return $r;
Knockout application code:
var VM = function(){
this.countries = ko.pureComputed(function(){$.getJSON("/index.php?r=countries/index").done(function(data){return data;});});};
If you watch from a browser(/index.php?r=countries/index), the array looks correct:
[{"id": 1, "name": "1"}, {"id": 2, "name": "2"}, {"id": 3, "name": "3"}]
But I can't figure out how to bind to observable array. How do I get data from the server and make them as observableArray.
A little race started. In my previous question I learned using knockoutjs to bind multiple lists by ID. The next step was to get the lists from the server (from json yii2). It seemed that everything should be very simple, but the anything do not work. Precisely here the question arose I decided to ask the local community. I searched so long for, was thinking how to do it, tried different options. Finally I found while initializing the app to load the list. Something like this:
var C = function(){
this.t = ko.observableArray();
var loadFrom = function(){
$.getJSON("someUrl", function(data){
$.each(data, function(index, value){
t.push({'id': value.id, 'name': value.name});
});
});
};
}
var VM = function(){
}
$(function(){
ko.applyBindings(new VM());
C.loadFrom;
})
It works! To initialize this method is suitable. But what if I need to load the list data at runtime of the application. To think again! Ohhh no! And then I found a great post by Christos S. He offers to bind ViewModel to a method in the following way:
<select data-bind:"optionsValue: 'AlbumId', event: { change: function (data, event) { showAlbumTracks($('#selectArtistAlbums').val(), data, event) } }"></select>
This is what I need! But I did not like that very cluttered HTML. But then I remembered one method of living examples on the knockout website.
self.category.subscribe(function() {
self.product(undefined);
});
All now I am satisfied. Well, almost... I don't like the solution with calling any methods after ko.applyBindings(). Can someone tell me how to make it better. The answer was confusing. Later I plan to put this on jsfiddle and share with the community.

Push new items into JSON array

Let's say this is the table inside my collection:
{
"_id" : ObjectId("557cf6bbd8efe38c627bffdf"),
"name" : "John Doe",
"rating" : 9,
"newF" : [
"milk",
"Eggs",
"Beans",
"Cream"
]
}
Once a user types in some input, it is sent to my node server, and my node server then adds that item to the list "newF", which is then sent back to my MongoDB and saved.
I'm trying to use update, which can successfully change the values inside of this table, but I'm not sure how to add new items onto that list. I did it with $push inside the MongoDB shell, but not sure how to do it on node.
Here's a snippet of my code:
db.collection('connlist').update({ _id: new ObjectId("e57cf6bb28efe38c6a7bf6df")}, { name: "JohnDoe", rating: 9, newF: ["Milk, Eggs", "Beans"] }, function(err,doc){
console.log(doc);
});
Well the syntax for adding new items is just the same as in the shell:
// make sure you actually imported "ObjectID"
var ObjectId = require('mongodb').ObjectID;
db.collection('conlist').update(
{ "_id": new ObjectId("e57cf6bb28efe38c6a7bf6df") },
{ "$push": { "newF": { "$each": [ "cream", "butter" ] } } },
function(err,numAffected) {
// do something in the callback
}
)
Or perhaps use .findOneAndUpdate() if you want to return the modified document instead of just making the alteration.
Of course use $push and possibly with $each which allows multiple array elements to be added when adding to an array. If you want "unique" items then use $addToSet where your operation allows.
And generally speaking for other items you should use $set or other operators in the update portion of your document. Without these operators you are just "replacing" the document content with whatever structure you place in the "update" portion of your statement.

Datatables process json response multiple objects

Im using Datatables Jquery plugin to do a bit of work, but i havent been able to understand how to process the Json response im getting from the server .
I get this response:
"aaData":[
[
{
"idTreatment":23,
"treatment":"Hospitalización",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":27,
"treatment":"Antibiótico",
"directions":null,
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":33,
"treatment":"Hidratación Oral",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
]
]
}
I have been trying to use mData or mRender, but im not familiarized with datatables yet, so i would be really grateful if someone could thell me how can in print this in a table on html
thanks in advance
this javascript fucntion is the one im using to initialize the table, im trying to use aoColumns based on examples but im not sure if this is the best option; also based on example im using mData, to show just 2 columns for test, as you can see im trying to acces the "array" or the json response using the objects field names, but when the page is rendering theres a warning saying that it cant find the field.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "mData": "idTreatment" },
{ "mData": "treatment" }
]
});
Well after looking several times over the API in the official website datatables.net i came to the conclusion that i had to test out.
If my problem isn't clear i will explain it again.
i have a table, and that table's data is obtained by a ajax request and a json response, the json response is a array of object but the objects themselves are composed of at least 2 objects ( {[[object1][object2]],[[object1][object2]]} ) so accessing them with mData was impossible at least for me, and the solution was so simple that i wonder i never tried, i just had to use mRender and a function.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "bVisible": false,
"mRender": function(data,type,full){
return data.idTreatment;}
},
{ "mRender": function(data,type,full){
return full[0].treatment;
} }
]
});
The function above its the one im using to initialise the table for the first time, i got the problem that i just wanted 2 field from the first object and none from the second one, but i dont now how to do it, so i used the "full" parameter that contains the whole json response, and accesed it.
This might not be the best answer but it did the work for me, so i hope that if someone know how to improved it , feel free to do it, or so just comment , and i also hope this can help someone else.

Sending complex JSON with fetch, save, and delete on a model or collection

We have an internal API that was specifically built to be used with a new piece of software I'm building that runs on Backbone. The API has a single URL and takes JSON as input to determine what it needs to return. It essentially allows me to build custom queries with JSON that return exactly what I'm looking for.
Thing is this JSON can get pretty verbose and is often 3–4 levels deep, but sometimes may just be a few lines and just 1 level deep.
First question first: How do I send a string of JSON along with the ID when I do a fetch()? Do I have to set these parameters as the model or collection's defaults?
Here is an example of a really simple string to get a specific user's info
{
"which" : "object",
"object" : {
"type" : "customer",
"place" : "store",
"customerID" : "14"
}
}
As others have suggested it will likely be challenging to work with SOAP, but it shouldn't be impossible. Backbone models and collections communicate with the server through the sync operation; you should be able to customize that. I think something along these lines might get the ball rolling (for models):
Backbone.SoapyModel = Backbone.Model.extend({
sync: function(method, model, options) {
// force POST for all SOAP calls
method = 'create';
options = _.extend(options, {
// Setting the data property will send the model's state
// to the server. Add whatever complexity is needed here:
data: JSON.stringify({
"which" : "object",
"object" : model.toJSON()
}),
// Set the request's content type
contentType: 'application/json'
});
// Defer the rest to Backbone
return Backbone.sync.apply(this, [method, model, options]);
}
});
var SoapyModelImpl = Backbone.SoapyModel.extend({
url: '/test'
});
var soapTest = new SoapyModelImpl({
id: 42,
name: 'bob',
address: '12345 W Street Dr',
phone: '867 5304'
});
soapTest.fetch();

get MongoDB results as objects instead of array

I'm new to MongoDB, and I'm trying to get results in a different way.
if I execute the query db.collection.find().toArray() I get something like:
[
{
"_id":"34234...",
"first":"Mark",
"last":"Marker"
},
{
"_id": "34235...",
"first":"Adam",
"last":"Smith"
}
]
is there an api that lets you to receive the results as the following?:
{
"results" : {
"34234..." :{
"_id":"34234...",
"first":"Mark",
"last":"Marker"
},
"4235..." :{
"_id": "34235...",
"first":"Adam",
"last":"Smith"
}
}
Or I need to get the results array and iterate every single object and build my response? (I would like to avoid the single cursor iteration)
I don't believe there's a native API function for that. cursor.toArray() goes through each item in the cursor begin with, so I wouldn't worry too much about that. We can just skip the toArray() and do our own iteration:
var obj = {}
db.collection.find().each(function(item){
obj[item._id] = item;
});
I don't think that would really be any slower.