These are chat archives for canjs/canjs

25th
May 2017
Yeswanth Raghav T
@tyraghav
May 25 2017 06:53
Hi
Hi
Hi
Yeswanth Raghav T
@tyraghav
May 25 2017 10:23

Hi
I am facing an issue while changing from model list
I have a js file which makes a service call using find all function of its model
For this json response it is working fine
{
"Output" : {
"Response" : [
{
"Name" : "tyraghav",
"Age" : "30"
}
],
"successful" : "true",
"returnCode" : "",
"returnMessage":""
},
"successful":"true"
}
For this json response it is not working
{
"Output" : {
"Response" :
{
"Name" : "tyraghav",
"Age" : "30"
},
"successful" : "true",
"returnCode" : "",
"returnMessage":""
},
"successful":"true"
}

Please help me with details of what comes in deferred and how it could be used
I am new to CAN js

Please help
Ivo Pinheiro
@ivospinheiro
May 25 2017 14:28
@phillipskevin, I've followed your suggestion and it is now working. Thanks
Kevin Phillips
@phillipskevin
May 25 2017 14:30
you’re welcome
Brad Momberger
@bmomberger-bitovi
May 25 2017 16:12
@tyraghav findAll() always expects your model data to be served as an array. If you only intend to serve one object, you should use findOne() to retrieve it.
Yeswanth Raghav T
@tyraghav
May 25 2017 17:30
this.options.coModel.attr("List", new MyModel(MyModel.findAll(name));
This is used to call the function from my JS file
thi
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:32
I don't think that's what you want. MyModel.findAll returns a promise, and so new MyModel(MyModel.findAll(name)) is constructing a single model from a promise object.
Yeswanth Raghav T
@tyraghav
May 25 2017 17:32
Ignore my above message please
this.options.coModel.attr("List", new MyModel.List(MyModel.findAll(name));
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:32
Same problem, just creating a List from a promise.
Or wait, that one may be supported. Let me check
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:41
OK, I misspoke about Model.Lists. Yes, you can do that, and from poking at it, I can see that if your promise resolves to a plain object, a list with a single element will be created.
What I suspect is happening, though, is that the single Response object can't be parsed by the MyModel.models()
you may have to override .models() in MyModel's static properties to account for cases where a single Response is sent back, not boxed into an array
Yeswanth Raghav T
@tyraghav
May 25 2017 17:44
Yes I changed it
But I am unable to find an alternative for this function call statement
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:48
Does your service sometimes return "Response": [{ ... }] and sometimes return "Response": { ... } ?
Yeswanth Raghav T
@tyraghav
May 25 2017 17:48
No
Earlier it was the array one
For which things are going fine
Now i want to change it to single value
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:50
MyModel.findOne() should be used in cases where the response is not an array.
So like this:
MyModel.findOne(name).then(function(instance) {
  this.options.coModel.attr("List", new MyModel.List([instance]); // or just assign instance to a key if you don't need a list.
}.bind(this));
If you need to have the list synchronously, for code simplicity:
this.options.coModel.attr("List", new MyModel.List());
MyModel.findOne(name).then(function(instance) {
   this.options.coModel.attr("List").push(instance);
}.bind(this));
Yeswanth Raghav T
@tyraghav
May 25 2017 17:54
No i don't want it to be a list
How can I do it with single value
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:55
MyModel.findOne(name).then(function(instance) {
  this.options.coModel.attr("Item", instance);
}.bind(this));
Yeswanth Raghav T
@tyraghav
May 25 2017 17:57
This function return a deferred object
What is the purpose of it
Is it the json we receive
Brad Momberger
@bmomberger-bitovi
May 25 2017 17:59
Deferreds are necessary to wait for asynchronous things to happen, like calls to a remote service.
So the thread can continue executing, and not block for a long time, but you have a reference that lets you continue later when the async thing has happened.
So the return of the JSON from the service triggers the resolution of an internal Deferred, and that is handled by turning the JSON into a MyModel instance, which is then what you get when you handle the Deferred from findOne.
(that's where .then() comes in)
Yeswanth Raghav T
@tyraghav
May 25 2017 18:07
Thank you very much
Frank Lemanschik
@frank-dspeed
May 25 2017 22:36
i know the chance is little but did some one here worked with ccTalk protocol befor?