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);
}
});
Related
I am running this script for getting all form fields of a indd file.
var _ALL_FIELDS = "";
var allFields = myDocument.formFields;
for(var i=0;i<allFields.length;i++){
var tf = allFields[i];
alert(tf.id);
alert(tf.label);
alert(tf.name);
alert(_ALL_FIELDS = _ALL_FIELDS +\",\"+ tf.name);
}
What i have done is, created a soap-java based client and calling the runscript method.
Now i am able to get these fields but how to send these fields back to the client i.e. how to write this in response and then at client side how to read it from response.
Code for calling runscript method is:-
Service inDesignService = new ServiceLocator();
ServicePortType inDesignServer = inDesignService.getService(new URL(parsedArgs.getHost()));
IntHolder errorNumber = new IntHolder(0);
StringHolder errorString = new StringHolder();
DataHolder results = new DataHolder();
inDesignServer.runScript(runScriptParams, errorNumber, errorString, results);
Also I have found in docs that runScript method returns RunScriptResponse but in my case it's returning void.
http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/server/ids-solutions.pdf
It looks like you want to return an array of formField names. You could take advantage of the everyItem() collection interface method in your javascript:
var result = myDocument.formFields.everyItem().name;
result;
The javascript will return the last value in the called script, so just to make it completely obvious, the last line is just the value to be returned.
On the Java side, the runScript method is passing the results variable as the 4th parameter, and that's where you'll find your response. So, after your code snippet, you might have something like this:
List<String> formFieldNames = new ArrayList<String>();
if (results.value.getData() != null) {
Data[] resultsArray = (Data[]) results.value.getData();
for (int i = 0; i < resultsArray.length; i++) {
formFieldNames.add(resultsArray[i].getData().toString());
}
}
I want to upload files to my server with CodeIgniter, and I need to change the file name dynamically with some random numbers.
But before changing the name I also need the original name to store it in the database. How can I do that?
Depending on which browsers you support you can use some of the functional extensions to the array prototype. Namely filter and every.
var arr = [[1,null,null],[1,null,null],[1,5,2],[null,null,null],[null,1,null],[1,2,3]];
var filtered = arr.filter(function(x){
return x.every(function(value){
return value != undefined;
});
});
console.log(filtered);
Just add these arrays to new one:
$newArray = [
$oldArray[4],
$oldArray[5],
$oldArray[7],
$oldArray[8],
$oldArray[10],
$oldArray[11],
];
EDIT
for dynamic, use something like that (not tested):
var newArray = [];
var oldArray = [/* ... */];
function addElement(eId){
newArray.push(oldArray[eId]);
}
$.each(SomeRandomArrayIndexes, function(k, eId){
addElement(eId);
});
Is there a way I can find out all elements of a given name in teh form history. In my firefox addon, I am adding some elements in the form-history under a specific name - lest say "search-description".
I now want to get all the elements I added under this name. I see that I can get a history object :
this.Ci = Components.interfaces;
this.Cc = Components.classes;
var historyObj = this.Cc["#mozilla.org/satchel/form-history;1"].getService(this.Ci.nsIFormHistory2 || this.Ci.nsIFormHistory);
But the nsIFormHistory or nsIFormHistory2 interfaces do not have any function like:
getAllEntries(name)
Anyone can help me out in this?
Usually, nsIFormHistory2.DBConnection property is used for querying, you access the SQLite table directly. Something like this (untested):
var completionListener =
{
handleCompletion: function(reason) {},
handleError: function(error) {},
handleResult: function(result)
{
var values = [];
while (true)
{
var row = result.getNextRow();
if (!row)
break;
values.push(row.getResultByName("value"));
}
alert("Autocomplete values: " + values);
}
};
var query = "SELECT value " +
"FROM moz_formhistory " +
"WHERE fieldname='search-description'";
var statement = historyObj.DBConnection.createAsyncStatement(query);
historyObj.DBConnection.executeAsync([statement], 1, completionListener);
Note that using async API is recommended here, querying the database might take time.
I'm working on an ASP.Net MVC application. My action is returning a view with a model that is an array of objects (a class with properties like Name, ID, IsViewable).
var model = #Model.ToJson(); // done via extension call
I want to observe this array, so whenever it changes I can update a table that has been bound to a template.
var viewModel = {
accounts = ko.observableArray(model)
}
This works just fine for adding and deleting elements from the array. However, I also want the template to update when a property in one of the accounts changes (ie, Name or ID).
On the KnockoutJS website, it says: Of course, you can make those properties observable if you wish, but that’s an independent choice. This is what I cannot figure out how to do.
I tried something like this with no avail:
var viewModel = {
accounts = ko.oservableArray([])
}
for(var i = 0; i < model.length; i++) {
ko.observableArray(model[i]);
viewModel.accounts.push(model[i]);
}
I can post the template and the table if it's needed.
You should look into the knockout.mapping plugin. I think it does everything you are looking to do.
I ended up getting this to work, so I thought I would share with anyone that might have having the same problem.
You need to wrap your array items in a JavaScript class. Then in the constructor, set each property to obserable:
var model = #Model.ToJson();
var viewModel = {
accounts = ko.observableArray(ko.utils.arrayMap(model, function(account) {
return new AccountWrapper(account);
}))
};
function AccountWrapper(account) {
this.Property1 = ko.observable(account.Propery1);
this.Property2 = ko.observable(account.Propery2);
this.Property3 = ko.observable(account.Propery3);
}
ko.applyBindings(viewModel);
And if you want to modify one of the items directly to see the change, you could do something like:
viewModel.accounts()[3].Name('My Name Changed');
And you can still get notified when items are added or remove:
viewModel.accounts.remove(viewModel.accounts()[4]);
Here's another approach that works and doesn't require the mapping plugin:
var model = #Model.ToJson();
var viewModel = {
accounts: ko.observableArray([]),
fromJS: function(js) {
for (var i = 0; i < js.length; i++) {
this.accounts.push({
Property1: ko.observable(js[i].Property1),
Property2: ko.observable(js[i].Property2),
Property3: ko.observable(js[i].Property3)
});
}
}
};
viewModel.fromJS(model);
ko.applyBindings(viewModel);
Is it possible to get the column names directly from a datacontext? Below I'm just hard coding the DataTextField and DataValueField, but I'd like to be able to get them from the datacontext if possible. What is the recommended way of getting the column names in a strongly-typed way?
DataClassesDataContext db = new DataClassesDataContext(WebConfigurationManager.ConnectionStrings[connstring].ConnectionString);
drdPriority.DataSource = db.Tasks;
drdPriority.DataTextField = "TaskDescription";
drdPriority.DataValueField = "TaskId";
drdPriority.DataBind();
Try the following code:
using (DataContext1.DataContext1 dc = new DataContext1.DataContext1()) {
var q = dc.Mapping.GetTables();
foreach (System.Data.Linq.Mapping.MetaTable table in q) {
List names = new List();
foreach (System.Data.Linq.Mapping.MetaDataMember member in table.RowType.DataMembers) {
names.Add(member.Name);
}
//Process the names here
}