Sencha2 JsonP push variable to Panel - sencha-touch

I am upgrading my app from Sencha 1 to Sencha 2 due to the fact that it handles JSONP a lot better.
I have my app set up and need to find out how I can push my JSONP results.
Here is my JSON request
var tweet = Ext.data.JsonP.request({
url: 'https://api.twitter.com/1/statuses/user_timeline.json',
params: {
'include_entities': true,
'screen_name': 'NynasBo',
'count': 1
},
callbackKey: 'callback',
success: function(data) {
Ext.each(data, function(i, item) {
var tweet = i.text;
});
}
});
And here is the function I am trying to push it too.
var contact = Ext.define('Nynas.view.Contact', {
extend: 'Ext.Panel',
xtype: 'contactcard',
config: {
title: 'Contact',
styleHtmlContent: true,
html: '' + tweet.success.tweet
},
});
But currently if I view my app I just get a undefined response.
I have verified the query and also made sure that it works, by simpling switching this line from
var tweet = i.text;
to
console.log(i.text);
You can see it will have a verified result

Remember, in an asynchronous environment, the callback will affect your value only after it has been used. So something like this should work:
Ext.define('Nynas.view.Contact', {
extend: 'Ext.Panel',
xtype: 'contactcard',
config: {
title: 'Contact',
styleHtmlContent: true,
html: ' '
},
});
var contact = Ext.create('Nynas.view.Contact');
var tweet = Ext.data.JsonP.request({
url: 'https://api.twitter.com/1/statuses/user_timeline.json',
params: {
'include_entities': true,
'screen_name': 'NynasBo',
'count': 1
},
callbackKey: 'callback',
success: function(result) {
contact.setHtml(result.data.text);
}
});

Related

Hapi.js reply.redirect() is not working after image upload

I have the following code, in my server. I'm uploading an image using mongoose and s3 and then want to redirect the user to another page but this isn't happening. (the upload is successful).
Routes.js:
{path: '/success', method: 'GET', config: controller.success} ......
controller.js:
imageUpload: {
payload: {
maxBytes: 209715200,
output: 'file',
parse: true
},
handler: function(request, reply) {
var userName = request.auth.credentials.username;
members.findMemberByUsername(userName, function(err, member){
if (err) {
return reply.view('upload', {error: err});
} else if (member) {
var IDImagePath = request.payload.uploadedIDname.path;
console.log(IDImagePath);
members.addID(member, IDImagePath, function(err1){
console.log("add id error", err1);
if (err1){
return reply.view('upload', {error: err1, member: member});
} else {
console.log("SUCCESSFUL!");
return reply.redirect('/success');
}
});
}
});
}
},
success: {
handler: function (request, reply){
request.auth.session.clear();
console.log("success handler working!!");
return reply.view('success');
}
}
The code hits both console.log("SUCCESSFUL") and console.log("success handler working!!") in the controller but the redirect doesn't take place. By the way I'm using 'Jade' as the templating language so I have a success.jade. Thanks.
I found out what the problem was. I'm using AJAX on the client side but didn't have a 'success' method to reload the page:
$('#submitID').click(function(){
var formData = new FormData($('#uploadID')[0]);
$.ajax({
url: '/api/image',
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
console.log(myXhr.upload);
}
return myXhr;
},
success: function(data) {
window.location.href = "/success"
},
data: formData,
cache: false,
contentType: false,
processData: false
}, "json");
});
I needed window.location.href = "/success" to reload the page. Please note the jQuery Ajax SUCCESS method is different to my '/success' route, they just happen to be the same word.

Uncaught Error: No module: users Angularjs

this is a follow up question of this How to fetch index data in Angular using rails server
Question
How to correctly upload / link angular module with html ?
Following the accepted answer and updating the code, I can not load module users:
Uncaught Error: No module: users
Gist from my project is here
updated code:
app.factory('User', function($resource) {
return $resource "users/:id", { id: '#id' }, {
index: { method: 'GET', isArray: true, responseType: 'json' },
show: { method: 'GET', responseType: 'json' },
update: { method: 'PUT', responseType: 'json' }
}
})
var UsersIndexCtrl = function($scope, User) {
$scope.users = User.index();
};
updated plunker according to the accepted answer is here
You have syntax errors with the following
return $resource "users/:id", { id: '#id' }, {
...
}
Should be
return $resource("users/:id", { id: '#id' }, {
...
});

i can load the model using direct but not from other server

**I am getting result for the below coding**
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'age'],
proxy: {
type: 'rest',
url : 'http://localhost:8085/sencha-touch-2.0.1.1-gpl/sencha-touch-2.0.1.1/docs/guides/data/examples/model_with_proxy/data/users/',
reader: {
type: 'json',
root: 'users'
}
}
});
**But when i try to get from other server i can't.I have tried this coding given below**
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'age'],
proxy: {
type: 'jsonp',
url : 'http://docs.sencha.com/touch/2-1/guides/data/examples/model_with_proxy/data/users/',
reader: {
type: 'json',
root: 'users'
}
}
});
var userStore;
Ext.require('Ext.data.Store');
Ext.onReady(function() {
// Uses the User Model's Proxy
userStore = Ext.create('Ext.data.Store', {
model: 'User',
autoLoad: true
});
// Gives us a reference to the User class
var User = Ext.ModelMgr.getModel('User');
var ed = Ext.create('User', {
name: 'Ed Spencer',
age : 25
});
// We can save Ed directly without having to add him to a Store first because we
// configured a RestProxy this will automatically send a POST request to the url data/users
ed.save({
success: function(ed) {
console.log("Saved Ed! His ID is "+ ed.getId());
}
});
// Load User 1 and do something with it (performs a GET request to /users/1)
User.load(1, {
success: function(user) {
console.log("Loaded user 1: " + user.get('name'));
}
});
});
I am getting the error
Uncaught TypeError: Object [object Object] has no method 'writeRecords' ext-all.js:18
Resource interpreted as Script but transferred with MIME type text/html: "http://docs.sencha.com/touch/2-1/guides/data/examples/model_with_proxy/data/users/?_dc=1352957198178&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback1". ext-all.js:18
Uncaught SyntaxError: Unexpected token :
If anyone knows can share with me

ExtJs:Initialize variable from Store

Hi i am using ExtHs 4.
I have a global variable which needs to be initialized from the store.
var cp=0;
Ext.onReady(function(){
Ext.define('Init', {
singleton: true,
cp: 0
});
Ext.define('loggedUserList', {
extend: 'Ext.data.Model',
fields: ['id','name']
});
loggedUser = Ext.create('Ext.data.Store', {
model: 'loggedUserList',
autoLoad: true,
proxy: {
type: 'ajax',
url : url+'/lochweb/loch/users/getLoggedUser',
reader: {
type: 'json',
root: 'provider'
}
},
listeners: {
load:function(loggedUser){
Init.cp = loggedUser.getAt(0).data.id;
}
}
});
});
i am using the value of cp in another url as follows,
Ext.define('vocbList', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', mapping: 'id' },
{ name: 'code', mapping: 'code' },
{ name: 'loinc', mapping: 'loinc.loincNumber' }
]
});
var vocabulary = Ext.create('Ext.data.Store', {
model: 'vocbList',
autoLoad: true,
proxy: {
type: 'ajax',
url : url+'/lochweb/loch/vocabulary/getVocabularyByProvider?providerId='+Init.cp,
reader: {
type: 'json',
root: 'Vocabulary'
}
}
});
but its value is still 0.How to assign its value form store so that it can be reused for other purpose.
Thanks
What you need is a callback.
Store call is being done asynchronously so that the browser will not freeze while the store get the data from the database.
me.vocabulary.load({
scope: this,
callback: function (records, operation, success) {
//here the store has been loaded so records are not empty
}
});
In your Ext.onReady, define a global class like this:
Ext.define('Init', {
singleton: true,
cp: 0
});
Then you can get or set cp anywhere you want:
Init.cp = 'some';
var myCp = Init.cp;

How to use api attribute on proxy

I would like to know how to use the api attribute of a proxy in ST2
For now, I have this in my proxy configuration:
api: {
create : App.urls.create_object,
read : App.urls.load_object,
update : App.urls.update_object,
destroy : App.urls.destroy_object
}
But then, I don't know how to use it.
For instance, when I wanted to create a new object, I created an Ext.Ajax.request with these parameters :
url: App.urls.create_object,
params: {
'object': object
},
But now, how could I do the same with the api attribute ?
Could you help ?
Assuming you have a model like this:
Ext.define('User', {
fields: ['name', 'email'],
proxy: {
type: 'ajax',
api: {
create: 'my_create_url',
read: 'my_read_url',
update: 'my_update_url',
destroy: 'my_destroy_url'
}
}
});
create
var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed#sencha.com'});
user.save(); // will POST to the create url
update
var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed#sencha.com'});
user.save({
success: function(user) {
user.set('name', 'Robert Dougan');
user.save(); // will PUT update URL
}
});
read
Using a store:
var store = Ext.create('Ext.data.Store', {
model: 'User'
});
store.load(); // will GET to read URL
Using the model:
// will GET the read URL with the specified ID.
User.load(12, {
success: function(user) {
console.log(user);
}
});
destroy
var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed#sencha.com'});
user.save({
success: function(user) {
user.destroy(); // will DELETE destroy URL
}
});
There is more information about this on the Rest proxy in the Sencha Docs: http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.Rest
sync
You can also use the store sync method to batch create/update/destroy all the records in your store.
var store = Ext.create('Ext.data.Store', {
model: 'User'
});
store.add({ name: 'Robert Dougan', email: 'rob#sencha.com' });
store.sync(); // will batch update all the needed records