Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should data in promise be formatted? #35

Closed
jhammer2 opened this issue May 28, 2015 · 12 comments
Closed

How should data in promise be formatted? #35

jhammer2 opened this issue May 28, 2015 · 12 comments

Comments

@jhammer2
Copy link

I am trying to use loadData with a promise. However, it does not load theater. I return 1 record for testing and the grid ends up showing 12 pages of blank data. Perhaps I am formatting the data incorrectly? The format that I am returning in the promise is:

{ data: [{"AccountName":"MyAccount","ContactNumber":"111-111-7890","AccountNumber":"123456789","UserName":"JaneDoe"}], itemsCount: [1] }

Is this not correct?

@tabalinas
Copy link
Owner

If you loading data by pages from server (pageLoading: true), then you should return data in format

{
    data: [items],
    itemsCount: amountOfItems
}

// for your case
{
    data: [{"AccountName":"MyAccount","ContactNumber":"111-111-7890","AccountNumber":"123456789","UserName":"JaneDoe"}],
    itemsCount: 1 // without array
}

In most cases it's enough to load all data (usually it works faster for not huge amount of data). In this case just return the array of items.

See following fiddle with two examples http://jsfiddle.net/tabalinas/6690vqaz/

@jhammer2
Copy link
Author

Thanks for the reply. I tried your suggestion, however it just keeps showing the please wait. When looking in the browser console I see the following error. I am also doing a console.logo on my promise data to see that it is formatted correctly. Any suggestions on how to resolve this?

{ data: [{"AccountName":"MySuperDuperAccount","ContactNumber":"123-456-7890","AccountNumber":"123456789","UserName":"RickyBobby","Password":"FightingChicken","DueDate":"2015-05-20","MonthlyPayment":"100.00"}], itemsCount: 1 }

jsgrid.core.js:492 TypeError: undefined is not an object (evaluating 'this.data.length')

@jhammer2
Copy link
Author

what I am trying to do is an asynchronous call and returning the data in a promise.

@tabalinas
Copy link
Owner

Please, provide entire grid config. Maybe you could modify my fiddle to demonstrate the problem.

@jhammer2
Copy link
Author

I have updated your fiddle. However, you will not see the error due to the DB not existing, but you are able to see the controller code that I am using. Perhaps I am doing something incorrectly there?

Thanks,
John

On May 29, 2015, at 12:56 AM, Artem Tabalin notifications@github.com wrote:

Please, provide entire grid config. Maybe you could modify my fiddle to demonstrate the problem.


Reply to this email directly or view it on GitHub #35 (comment).

@tabalinas
Copy link
Owner

Send me the link to the updated fiddle then.

@jhammer2
Copy link
Author

Here is the link:

http://jsfiddle.net/6690vqaz/1/

On May 29, 2015, at 1:20 PM, Artem Tabalin notifications@github.com wrote:

Send me the link to the updated fiddle than


Reply to this email directly or view it on GitHub #35 (comment).

@tabalinas
Copy link
Owner

The promise should be resolved an actual JS object, not a string representation

feeds = '{ data: ['+feeds+'], itemsCount: '+len+' }';
def.resolve(feeds); // feeds is a string, but should be an object

Should be

feeds = {
    data: [items] // provide array, not a string here too
    itemsCount: len
};
def.resolve(feeds);

Hope it will help.

@jhammer2
Copy link
Author

That is very helpful. I am now getting the screen to come up, however the cells are blank. When looking in the browser console I do the javascript object. can you tell if I am loading the object incorrectly? I am putting the array in, but perhaps the format is incorrect?

[Log] Object (roa.js, line 57)
data: Array[1]
0: "{"AccountName":"MySuperDuperAccount","ContactNumber":"123-456-7890"}"
length: 1
proto: Array[0]
itemsCount: 1
proto: Object

Here is the code that I used to create the object:

function getRecords() {
var feeds = "";
var def = $.Deferred();
var db = window.openDatabase("Database", "1.0", "MyDB", 200000);

                                        db.transaction(function(tx) {
                                        var db = window.openDatabase("Database", "1.0", "MyDB", 200000);

                     tx.executeSql("SELECT AccountName,ContactNumber FROM BILL_TABLE", [], function(tx, results) {

                                                                var len = results.rows.length;
                                                                var feedarray = [];

                                                                for (var i = 0; i <=len-1; i++) {

                                var len = results.rows.length;
                                                                    var roaObj =results.rows.item(0);
                                                                    var jsonString=JSON.stringify(roaObj);

                                                                    feedarray.push(jsonString);
                                                                }
                                console.log(feedarray);

                                                                  feeds = {
                                                                            data: feedarray, // provide array, not a string here too
                                                                            itemsCount: len
                                                                    };
                                                                    console.log(feeds);
                                                                  def.resolve(feeds);
                                                            });
                                        });
                                return def.promise();
                        }

Thanks,
John

On May 29, 2015, at 3:42 PM, Artem Tabalin notifications@github.com wrote:

feeds = {
data: [items] // provide array, not a string here too
itemsCount: len
};
def.resolve(feeds);

@tabalinas
Copy link
Owner

You are right, the format of objects in array is incorrect. You provide them as strings, but elements of data array should be also js objects. You can see the problem in console, where object is printed in quotes.

@jhammer2
Copy link
Author

jhammer2 commented Jun 1, 2015

Thanks so much for your help. It is now working for me.

On May 31, 2015, at 5:27 PM, Artem Tabalin notifications@github.com wrote:

You are right, the format of objects in array is incorrect. You provide them as strings, but elements of data array should be also js objects. You can see the problem in console, where object is printed in quotes.


Reply to this email directly or view it on GitHub #35 (comment).

@tabalinas
Copy link
Owner

You are always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants