indrimuska / angular-selector

A native AngularJS directive that transform a simple <select> box into a full html select with typeahead.
http://indrimuska.github.io/angular-selector
MIT License
96 stars 36 forks source link

Issue with JSON Object and arrays #6

Closed vivek-mdrift-personal closed 8 years ago

vivek-mdrift-personal commented 8 years ago

Dear Indri Muska,

I was trying with your API example as given below.... $scope.remoteConfig = { url: "http://services.groupkt.com/country/search", transformResponse: function (data) { console.log(typeof data); // var str = data; // var json = JSON.stringify(eval("{"+"tags:" + str + "}"));
// var myJsonString = JSON.stringify(yourArray); // var catcher = JSON.stringify(data);
// var catcher = JSON.stringify({data}); var catcher = { 'tags': JSON.parse(data) }; console.log(typeof catcher);
var countries = angular.fromJson(data).RestResponse.result; return countries.map(function (country) { return { name: country.name, code: country.alpha2_code }; }); } };

However, I could not maniputate the array with catcher, which is a JSON object converted from an JSON array. Is it necessary that we need to convert the JSON array to JSON Object? The code pasted above is giving me errors...

Thanks.

Regards, Vivek Elayidom

indrimuska commented 8 years ago

Hi @mdrift-central, I don't understand why you're saying that is necessary to convert the JSON array to a JSON object, which object/array are you talking about? I can't see any error with the code you posted.

However, if you want to manipulate the result array data with any of your functions (catcher) you simply have to manipulate the parsed data from a valid JSON string:

transformResponse: function (string) {
  // parse JSON string -- similar to JSON.parse
  var myData = angular.fromJson(data);
  // manipulate your data
  var result = myData.RestResponse.result;
  var countries = result.map(function (country) {
    return { name: country.name, code: country.alpha2_code };
  });
  // apply other functions
  // ...
  // return countries array
  return countries;
}

Let me know if this could help you. :)

vivek-mdrift-personal commented 8 years ago

Thanks Indri Muska,

Your suggestion helped me a lot... Thanks and keep up the good work... So far, I found your library the best for tagging..

All the very best.

Regards, Vivek

vivek-mdrift-personal commented 8 years ago

Also, how do I call multiple RemoteConfig functions? Any suggestions? I mean - other than separate controllers...

indrimuska commented 8 years ago

Glad to see you've solved this issue. :smile:

With "multiple RemoteConfig functions", do you mean a kind of dynamic transformation callback for your ajax responses? If so, remember that the remoteConfig property is the same object passed to the native Angular $http service (docs here), so you can use any valid options the service have.