I have array of elemnts in parse. each object has 2 members.
date and pointer to user.
I make query to object that have the array as a field. How can I include this array that I will receive also the users and not the pointers?
Array :
[{date:dateObject,user:pointer},....]
I know about
query.include(' ')
but how do I make it for array?
Thanks
You will have to fetch them manually, include doesn't work here.
Try something like this:
query.find().then(function(results) {
// get array of pointers to users
var arrayOfPointers = [];
for (var i=0; i<results.length; i++) {
// we need the second element of each object
arrayOfPointers.push(results[i][1]);
}
// now fetch all the users
Parse.Object.fetchAll(arrayOfPointers).then(function(list) {
// do something with list of fetched users
});
});
Related
I've got a subscriptionObserver including the following:
return update(
previousResult,
{
ApptsForCurrentUser: {
$push: [newAppt],
},
}
);
The new item that arrives via the subscription needs to be inserted into the ApptsForCurrentUser array in date-sorted order. It's a multi-dimensional array, and I can sort it using an $apply function.
Is there syntax to $push newAppt to the array prior to handing the array off to the $apply function that will sort it?
Alternatively, should I do something like this?
(Not yet tested):
var newResult = clonedeep(previousResult); //lodash
newResult.push(newAppt);
newResult.sort(myCustomSortFunction);
const newResultAsAConst = clonedeep(newResult);
return update(
previousResult, newResultAsAConst
);
Building on advice from #Sacha, I was able to solve this via this code:
import update from 'immutability-helper';
[.....]
const resultWithNewApptAdded = update(previousResult, {getAllApptsForCurrentUser: {$push: [newAppt]}});
//getAllApptsForCurrentUser contains unsorted list of appts
resultWithNewApptAdded.getAllApptsForCurrentUser = resultWithNewApptAdded.getAllApptsForCurrentUser.sort(mySortFunction);
return resultWithNewApptAdded;
I have some textboxes generated dynamically through Ajax. I'm using Jackson 1.9.8 to parse json. I can retrieve the values of those dynamic textboxes using jQuery as follows.
var itemsArray=[];
$('input[name="txtChargeSize[]"]').each(function(){
itemsArray[i][2]=$(this).val();
});
This can retrieve each element of the textbox array txtChargeSize[] one by one.
itemsArray is an array which already holds
The value of weightId on the itemsArray[i][0] position,
The value of weight on the itemsArray[i][1] position,
The value of charge on the itemsArray[i][2] position which is the value of the textbox being assigned in the above code.
I need to pass this array to Spring controller class to insert these values into the Oracle database. I'm trying the following.
var i=0;
$('input[name="txtChargeSize[]"]').each(function(){
itemsArray[i][2]=$(this).val();
objectArray[i]=[["weightId",itemsArray[i][0]], ["weight",itemsArray[i][1]], ["charge",itemsArray[i][2]]];
i++;
});
It doesn't work as I expect. I need to pass something like the following.
[["weightId", 1], ["weight", 12.4], ["charge", 15.5]]
so that it can be parsed to java.util.List<Object[]>. I don't have precise knowledge of Javascript to accomplish this. How can I pass in this way the values held by itemsArray to Spring controller using json?
var i=0;
$('input[name="txtChargeSize[]"]').each(function(){
itemsArray[i][2]=$(this).val();
i++;
});
try javascript constructor method like this.
for(var i = 0; i < itemsArray.length; i++) {
objectArray[i]= new createObj(itemsArray[i][0], itemsArray[i][1], itemsArray[i][2]);
}
and the constructor function is
function createObj(weightID, weight, charge) {
this.weightId = weightID;
this.weight = weight;
this.charge = charge;
}
and finally if you want json string then use this
var str = JSON.stringify(objectArray);
alert(str);
I have an array containing objects. Each object has an id field. I want to implement a function that deletes an object by specifying the id. I use .each from prototypejs on the array to step through the objects and check the id. If it matches, how do I actually delete it? I've tried setting the object returned from .each to null, but using FireBug I see that the object is still the same in the array.
EDIT: the objects in the array may in turn contain arrays with objects that may need to be deleted. My function is fine for finding the object to be removed, and I use splice to remove it (using a counter). It seems to me that .each (and the other enumerators like .reject) returns a copy of the object. If I set the object to null then upon inspection the object is still in the array. How would I return a reference of the object that when set to null would actually operate on the object in the array, and not a copy?
Here is the function, the deleteChild function works on the same principal:
function removeControl(controlName) {
var counter = 0;
cont.each(function (existingControl) {
if (existingControl.id == controlName) {
existingControl.destroy();
cont.splice(counter, 1);
}
else { // not found, check control's children
existingControl.deleteChild(controlName);
}
counter++;
}, this);
}
Only use .each when you want to do something to every object. Semantically speaking, you should be using Enumerable.reject in this situation. Think how much easier it will be to understand when you have to fix it in years' time.
function deleteById(objects, id) {
return objects.reject(function(obj) {
return obj.id == id;
}).each(function(obj) {
obj.deleteChild(id);
});
}
I want to pass to create a json output with a dictionary with multiple models, like this:
results = {}
results["game_info_db"] = db.gameInfo.objects.get(name='name')
results["dlc_list_db"] = db.gameAddon.objects.filter(game__name='name')
What i tried is serialize (serializers.serialize) all dicts entrys and after this i dumps (simplejson.dumps) all the dict... but it doesn't seems to be correct ...
any sugestion ?
You could pass in the values of the models and convert it to a list:
results = {}
results["game_info_db"] = list(db.gameInfo.objects.get(name='name').values())
results["dlc_list_db"] = list(db.gameAddon.objects.filter(game__name='name').values())
return HttpResponse(json.dumps(results), mimetype='application/javascript')
The data will appear as objects on the javascript side. Assuming you have a name column, you can access the attributes like the following:
$.getJSON("/ajax/", function(data) {
var dlcs = data.dlc_list_db;
for (i = 0; i < dlcs.length; i++) {
var dlc = dlcs[i];
alert(dlc.name);
}
});
I am trying to access an object with form data sent to my controller. However, when I try to access objects I get values of null or 0. I used two methods, the first by serializing and the second by storing names and values in one object. (the code below sends/posts serialized)
Here is my JS...
$("#createUser").click(function() {
//store input values
var inputs = $('#newUserForm :input');
var input = $('#newUserForm :input').serializeArray();
console.log(input);
//if I want just the values in one object
var values = {};
$(inputs).each(function() {
values[this.name] = $(this).val();
});
console.log(values);
if(LiveValidation.massValidate( validObj )){
$.post('./adminPanel/createUser', function(input){
alert('Load was performed.');
//test confirmation box
$("#msgbox").html("Grrrrreat");
//drop down confirmation
$("#msgbox").slideDown();
});
} else {
//test fail box
$("#failbox").html("Fail");
$("#failbox").slideDown();
}
});
In the controller side I try to access data the following way...
$this->input->post("firstName")
where firstName is the name of the field.
Below is an image of the objects passed.
Top being serialized array and the bottom a single object with all the names and values of form...
If you're using jQuery, you can use jQuery's built in serialize/query string functions to get the data from a form: http://api.jquery.com/serialize/
In your case:
var data = $('#newUserForm').serialize(); // is a string like "firstName=jon"