I was using this code to get data from facebook api and to be shown using Durandal, and it is working fine.
return $.ajax({
url: '=?',
dataType: 'json',
}).promise();
I was trying to do the same using getJSON
$.getJSON( "=?");
I tried the following code; it didn't work.
var jqxhr = $.getJSON("=?");
return jqxhr;
This gets me the data from the facebook.
But the issue is in returning the promise. Since I'm using durandal it is important to return a promise for the module to work.
How to return a promise while we use getJSON
?
This my durandal module
define(function (require) {
return {
getCustomers: function () {
//do some ajax and return a promise
var jqxhr = $.getJSON("=?");
return jqxhr;
}
};
});
This is where I call it
define(function (require) {
var backend = require('backend');
var ko = require('knockout');
return {
customers: ko.observableArray([]),
activate: function () {
var that = this;
return backend.getCustomers().then(function (result) {
var arr = [];
arr.push(result)
that.customers(arr);
});
}
};
});
I was using this code to get data from facebook api and to be shown using Durandal, and it is working fine.
return $.ajax({
url: 'http://graph.facebook./facebook?callback=?',
dataType: 'json',
}).promise();
I was trying to do the same using getJSON
$.getJSON( "http://graph.facebook./facebook?callback=?");
I tried the following code; it didn't work.
var jqxhr = $.getJSON("http://graph.facebook./facebook?callback=?");
return jqxhr;
This gets me the data from the facebook.
But the issue is in returning the promise. Since I'm using durandal it is important to return a promise for the module to work.
How to return a promise while we use getJSON
?
This my durandal module
define(function (require) {
return {
getCustomers: function () {
//do some ajax and return a promise
var jqxhr = $.getJSON("http://graph.facebook./facebook?callback=?");
return jqxhr;
}
};
});
This is where I call it
define(function (require) {
var backend = require('backend');
var ko = require('knockout');
return {
customers: ko.observableArray([]),
activate: function () {
var that = this;
return backend.getCustomers().then(function (result) {
var arr = [];
arr.push(result)
that.customers(arr);
});
}
};
});
Share
Improve this question
edited Nov 5, 2013 at 6:27
Okky
asked Nov 5, 2013 at 6:21
OkkyOkky
10.5k15 gold badges77 silver badges123 bronze badges
3 Answers
Reset to default 3This should work
$.getJSON( "http://graph.facebook./facebook?callback=?").promise();
it's already with promise interface : "This jQuery XHR object, or "jqXHR," returned by $.getJSON() implements the Promise interface"
Look here at the example :
var a=$.getJSON( "http://graph.facebook./facebook?callback=?");
a.done(function (){...}).fail(function (){...}).always(function (){...});;
ps
"This gets me the data from the facebook."
NO. this returns jqXHR object.
This has done the trick. Thank you guys
return $.getJSON( "http://graph.facebook./facebook?callback=?").done();