I'm using Titanium Appcelerator, and Node.ACS to manage my server pages. I must use some APIs to update objects, which basically are JSON objects.

I want to do a function to update one field of an object, but don't know how dinamically construct the JSON config.

This is my code:

function (id, field, value) {
    ACS.Objects.update({
        classname: MyObject,
        id: id,
        fields: {
            HERE_MUST_GO_THE_FIELD_NAME: value
        }
    }, function(data) {
        if (data.success) {
            console.log('Done');
        } else {
            console.log('Error');
        }
    });
}

I know that I can create a property dinamically with:

var foo = {};
var bar = 'baz';
foo[bar] = '123';

But in this case I cannot make something like:

    ACS.Objects.update({
        classname: MyObject,
        id: id,
        fields[field]: value
    }, function(data) {

Because it throws an error. So, is there a way to do this?

NOTE: Obviously, make:

        fields: {
            field: value
        }

is not working, because works as literal.

有帮助吗?

解决方案

Ok, I'm almost stupid (almost, because I realized by myself the solution).

It's the same pattern as standart, but more deep:

                var config = {
                    classname: MyObject,
                    id: id,
                    fields: {}
                };
                config.fields[field] = value;
                ACS.Objects.update(config, function(data) {

I leave it here for others newbies like me.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top