5

I'm trying to asynchronously send some data as a single object. Half of the data is coming from my KnockoutJS viewModel. The other half is some data that I want to add on to that.

My thought was to convert them both to JSON objects, then use an array .concat to put them together. But this isn't working. Might you know why?

I've tried a few solutions. The first method builds an object from a JSON string, and then uses JSON.parse to put them as an object. The second tries to avoid strings altogether. Either way, after I get my objects I try concatenating them together, but without any luck.

With Strings

toAddString = '{"file": "thefile"}';
toAddObj = JSON.parse(toAddString);

koString = ko.toJSON(viewModel);
koObj = JSON.parse(koString,null,2);

finalObj = koObj.concat(toAddObj);

With Objects

toAddObj = [{"file": "thefile"}];

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

With Objects (2)

toAddObj = new Object();
toAddObj.file = "one";

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

Do you know what might be going wrong here?

All I want is a single object, be it an array or a JSON object, that contains the data from each of these sources.

3
  • 1
    What does ko.toJS(viewModel) give you as a result? Is it an array, or an object? Are you trying to end up with a single object with the combined properties of toAddObj and koObj? Or did you want an array? Commented Dec 20, 2012 at 0:03
  • @GregL, I updated the question. I don't really care if I get an array or an object back; either would work. ko.toJS() works by creating a JSON string of the viewModel, then running the browser's native serializer on it, so it should be a JavaScript object. Commented Dec 20, 2012 at 0:17
  • What does not work? What exceptions do you get? Commented Dec 20, 2012 at 0:48

2 Answers 2

7

Try the following. I am guessing at the syntax, since I don't use Knockout myself, and I am using the ko.utils.extend() function to copy the properties of one object onto the other.

var toAddObj = { file: 'one' };

var koObj = ko.toJS(viewModel);

var finalObj = ko.utils.extend(toAddObj, koObj);

Note that without using var you are always creating global variables (typically a bad idea).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was looking for a utility method called something like "extend" - many JS libraries have one, such as jQuery, AngularJS, UnderscoreJS and probably others. They all work pretty similarly to the Knockout one.
2

Check the types of your variables:

/* With Strings */
toAddString = '{"file": "thefile"}'; // a string
toAddObj = JSON.parse(toAddString); // an object

koString = ko.toJSON(viewModel); // a string containing JSON
koObj = JSON.parse(koString,null,2); // an object
                                     // notice JSON.parse does only take one argument

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

/* With Objects */
toAddObj = [{"file": "thefile"}]; // an object (represented in code as literal)

koObj = ko.toJS(viewModel); // an object

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

/* With Objects (2) */
toAddObj = new Object(); // an object
toAddObj.file = "one"; // with a string property

koObj = ko.toJS(viewModel); // an object

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

So, if ko.toJS(viewModel) returns an object which is not an array, you will get lots of "no method concat on …" exceptions. Instead, you could just put both of them into an array:

[toAddObj, koObj] // and JSON.stringify that

or you go with a string building process and use

"["+toAddString+","+koString+"]";

where the first method is preferable.

1 Comment

+1 for the nice answer, Bergi. Very enlightening! I was clearly confusing arrays and objects, here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.