0

Actually i am trying to populate a list of years using canjs.

CATEGORIES is the existing json object

var CATEGORIES = [
    {
        id: 1,
        year: '1983'
    },
    {
        id: 2,
        year: '1984'
    },
    {
        id: 3,
        year: '1985'
    }
];

and remaining from database using can.ajax and slim frame work.... i can able to get the details from database using can ajax and json encode...now my problem is how can i add these fetched json array to existing json object...i tryed below code..

var CATEGORIES = [
    {
        id: 1,
        year: 'Family'
    },
    {
        id: 2,
        year: 'Friends'
    },
    {
        id: 3,
        year: 'Co-workers'
    }
];

can.ajax({
url: 'api/years',
error: function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.status);
                   alert(thrownError);
alert(xhr.responseText);
                 },
success: function(CATEGORIES1) {
alert(CATEGORIES1); // output: [{"id":"1","year":"1986"},{"id":"2","year":"1987"}]
CATEGORIES = JSON.stringify(eval("(" + CATEGORIES1 + ")"));
alert(CATEGORIES); // output:[{"id":"1","year":"1986"},{"id":"2","year":"1987"}]
}
});


alert(CATEGORIES); // output:[object Object],[object Object],[object Object]
sasi kanth
  • 2,637
  • 4
  • 17
  • 29
  • That's not [JSON](http://json.org), it's JavaScript. Ex.: *"CATEGORIES is the existing json object"* No, it's a JavaScript array. – T.J. Crowder Sep 20 '13 at 09:23
  • how can i add new values in that – sasi kanth Sep 20 '13 at 09:25
  • @ sasi: Adding entries to an array is really basic. I suggest stepping back from what you're doing and working through some good JavaScript tutorials. But: `CATEGORIES.push({id: 4, year: '1986'});` – T.J. Crowder Sep 20 '13 at 09:26
  • @ T.J.Crowder: i tried that also but not useful – sasi kanth Sep 20 '13 at 09:28
  • @jandjorgensen this question is not for jquery . i don't know why u modify my code we are working in canjs – sasi kanth Nov 05 '14 at 06:20
  • @sasikanth The problem you are experiencing is related to AJAX and JavaScript, not specifically CanJS. Stack Overflow questions are meant to be useful to future readers, and it is common to remove irrelevant information for that reason. If it is a CanJS-specific problem, you should provide further information to demonstrate that. – ramblinjan Nov 05 '14 at 20:07
  • i know ajax..the problem is how to save values in canjs itself k. – sasi kanth Nov 06 '14 at 05:20
  • What do you mean by "how to save values"? The question in its current state looks like it's about parsing. – ramblinjan Nov 10 '14 at 17:05

1 Answers1

1

I believe what you want is to concat you existing array with the new one returned using ajax:

CATEGORIES = CATEGORIES.concat(CATEGORIES1)
Sebastian
  • 2,249
  • 17
  • 20