How to dynamically bind images from store to carousel in sencha touch - sencha-touch

I have the following Store declared in my sencha touch application
Ext.define('Sample.store.ImageStore', {
extend: 'Ext.data.Store',
config: {
model: 'Sencha.model.ImageModel',
data: [{ name: "cat", url: "http://bleachthemind.files.wordpress.com/2010/08/cute-bunnys-domestic-animals-2785589-1024-768.jpg" },
{ name: "lion", url: "http://images1.fanpop.com/images/photos/2600000/Cheetah-Family-wild-animals-2603080-1280-1024.jpg" }
]
}
});
This is my code being declared in Model:
Ext.define('Sample.model.ImageModel', {
extend: 'Ext.data.Model',
config: {
fields:['name','url']
}
});
I am facing difficulty to construct a view with carousel where data is being binded from the store mentioned above.Please can i know the right syntax to be written in the view with the carousel consuming store data.

You cannot hook up Store into Carousel in Sencha Touch. It seems that you have to manually do it through some way like this:
yourCarousel = Ext.getCmp('your_carousel_id');
store.each(function(record){
yourCarousel.add({
html: '<img src=' + record.get('url') + '/>'
});
});

Thiem's answer is fine.
If you want a more complete example, have a look at this nice post:
http://edspencer.net/2012/02/building-a-data-driven-image-carousel-with-sencha-touch-2.html
I think it should cover all your needs ;)
Hope this helps.

Related

Retrieve Project Name

How do you get the project name in Rally?
I'm working with a grid app and all I'm trying to do is include a 'Project' field for the grid view. However, because 'Project' is actually an object, the resulting field is '[object Object]'. So, how is it possible to get the name in string type?
Here's the code from my columnCfgs that deals with making the field.
{
text: 'Project',
dataIndex: this.getContext().getProject().get
},
Try this.getContext().getProject()._refObjectName or this.getContext().getProject().Name
In some cases it is useful to print and explore the object in the console, because it may be that you need to traverse project.data._refObjectName as in this gist, or in your case:
Ext.define('CustomApp', {
extend: 'Rally.app.App',
componentCls: 'app',
items:{ html:'App SDK 2.0 Docs'},
launch: function() {
var currentProject = this.getContext().getProject();
console.log(currentProject);
this.add({
xtype:'container',
html: currentProject.Name
});
}
});

Store contains only data inside "raw" property

I'm having strange problem with my store inside ExtJS. My ASP.NET MVC3 controller returns JSON:
My store:
Ext.define('MyApp.store.Users', {
extend: 'Ext.data.Store',
config: {
// I know the model works
model: 'MyApp.model.User',
storeId: 'Users',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'users/read',
reader: {
type: 'json',
root: 'users'
// also tried this
rootProperty: 'users'
}
}
}
});
Now, when I connect this store to the grid inside ExtJS 4.2.1, the grid shows TWO rows but without data. When I console.log(store) I see the data only inside raw property, not inside data property.
Does anyone know what's the problem? Why isn't there any mapping? The grid's dataIndex is also the same as Models fields (I've done this a thousand times with PHP, I don't know where is the problem here.)
One more thing I've tried. I've tried renderer: function(value) { console.log(value); } inside grid's columns and I was just getting undefined.
Edit: this is how the JSON actually looks like:
Try using root: 'users' not rootProperty. If not specified root defaults to ''.
Sencha Docs
SENCHA what the hell?! Sencha Touch 2 always says put everything in config?
Now when I do that in ExtJS, everything breaks?
I removed everything from config: {} and now it works great.

Event SelectField Sencha Touch 2.1 and Using Store and Model in it. (in Sencha Architect 2)

I begin learn about Sencha Touch 2. So, I have had many problems to ask! ^^ Let's research it.
Now I have a data json like:
{
result: "SUCCESS",
national: [
"Afghanistan",
"Albania",
"Albania",
"Algeria",
"American Samoa",
"Andorra"
]
}
Then, I will load it from url: nation.php file.
How can i load it to My Select Field.??????
Share and Support to me.! Thanks :).
I don't know how to do this in Sencha Architect 2 ( i am not using it).. but still
Instead of asking question without try (I mean you didn't post tried code here), Better you start with Sencha Touch Documentation.
Anyway, you can do it as follows
Model
Ext.define('AppName.model.countries', {
extend : 'Ext.data.Model',
config: {
fields: [
{name: 'name', convert: function(value, record) {
return record.raw;
}}
],
}
});
Store
var myStore = Ext.create("Ext.data.ArrayStore", {
model : 'AppName.model.countries',
proxy: {
type: "ajax",
url : "nation.php",
reader: {
type: 'json',
rootProperty : function(data) {
return data.national;
}
}
},
autoLoad: true
});
Select Field in View
Ext.create('Ext.form.Panel', {
fullscreen: true,
items: [{
xtype: 'selectfield',
store: myStore ,
valueField:'name',
displayField:'name'
}]
});
With Viswa's Support. :) I found this problem - XMLHttpRequest cannot load. Origin is not allowed by Access-Control-Allow-Origin error (browser policy security).
And Sencha Touch document say: " The JsonP proxy is useful when you need to load data from a domain other than the one your application is running on. If your application is running on http://domainA.com it cannot use Ajax to load its data from http://domainB.com because cross-domain ajax requests are prohibited by the browser.
" Also, All we need to do is - "Implement all api in Your Webserver" and Follow JsonP's format code: ( in PHP)
$callback = $_REQUEST['callback'];// check callbackkey
// Create the output object.
$output = array('a' => 'Apple', 'b' => 'Banana');// output data.
//start output
if ($callback) {
header('Content-Type: text/javascript');
echo $callback . '(' . json_encode($output) . ');';
} else {
header('Content-Type: application/x-json');
echo json_encode($output);
}
If. Using Sencha Touch 2.1, You can use:
Ext.data.JsonP.request({
url: 'http://otherdomain/svn_visaapi/trunk/api/visa_api.php/test_json',
callbackKey: 'callback',
success: function(result) {
console.log(result);
//Your success function here...
}
});
- If, Using Sencha Architect, you can use a Store.proxy.JsonP to call api.
- Read more document Sencha Touch 2.1 to see that.

Sencha Touch 2 link UI element which can be clicked

See attachment, I want the "Forgot password" to be a link instead of a button, but I cant find any Link UI element in Sencha which listen for the click event, doesnt it exist?
You can simply use a component and define the html for it, please see the code below:
{
xtype: 'component',
html: '<a href='+'"http://abc.com"'+ '>Forgot your password?</a>',
}
You can use Sencha routes, e.g.:
Ext.define('MyApp.controller.User', {
extend: 'Ext.app.Controller',
config: {
routes: {
'forgot-password': 'forgotPassword'
}
},
forgotPassword: function() {
// Your code here
}
});
Then you can formulate the link using the route name after a hashtag e.g.:
http://myapp.com#forgot-password
More on routes here: http://docs.sencha.com/touch/2-0/#!/guide/history_support

Sencha touch 2 : List shows only last item retrieved from API

I want to show the data from the following API in List :
http://customer.appxtream.com/astro.apps.cms/jsonFeed.action/?service=astroSportsDataService&action=grabJsonText&mimeType=application/json&p1=HighlightNewsList&p2=EURO2012
when I get the data, the Network of Chrome shows me that all elements have been retrieved but List shows only the data from the last item in the API here is my code:
View
Ext.define('astro.view.HighliteNews', {
extend: 'Ext.DataView',
xtype: 'highliteNews',
config:{
title:'xren',
store: 'highliteNewsStoreId',
itemtap: true,
scrollable:'horizontal',
inline:{
wrap:false
},
itemTpl:[
'<div><img src="{imageLink}"/> </div>',
],
},
});
Store
Ext.define('astro.store.HighliteNewsStore',{
extend: 'Ext.data.Store',
xtype:'highliteNewsStore',
config:{
model: 'astro.model.HighliteNewsModel',
autoLoad: true,
storeId: 'highliteNewsStoreId',
proxy:{
type:'ajax',
url :'http://customer.appxtream.com/astro.apps.cms/jsonFeed.action',
extraParams:{
service:'astroSportsDataService',
action:'grabJsonText',
p1:'HighlightNewsList',
p2:'EURO2012',
mimeType:'application/json'
},
reader:{
type:'json',
rootProperty:'cmsHighlightNewsList',
},
},
}
});
Model
Ext.define('astro.model.HighliteNewsModel',{
extend: 'Ext.data.Model',
config:{
fields: ['imageLink'],
}
});
So the network shows that 3 images are sent from API but the List shows ONLY LAST IMAGE.
Please help
I solved the problem guys :) , there is an conflict between Sencha and JsonP, so if you assign a idproperty to your Model, the problem will be sloved :
config:{
idProperty: 'HighliteIdProperty',
fields: ['imageLink'],
}
A few things I would suggest :
Add a listener to the load event of the store
This is just to check that all the data is load in your store.
You can do it in different ways :
In a controller
store.on({
load:function(){
console.log(store.getCount());
}
});
or directly in the store config with the listeners attribute
listeners:{
load:function(store){
console.log(store.getCount());
}
}
Check the DOM
In the Web Inspector / Firebug, check if your items are there or only the last one.
I think you could also try this command in the console :
Ext.DomQuery.select('div[class=x-dataview-item]').length
It will return the number of items in your dataview.
Set a width and height to your image
I also had the same problem with a dynamically loaded dataview. All the picture were there in the DOM but too small to be seen. So I suggest, you give them a CSS class with a defined width and height.
That's all I can think of for now.
Hope this helps
Thanks Arash for this question and answer. We are facing similar problem and found that we didn't have model defined. Defining the model worked in our case. However, it worked while we retrieved playlist from youtube and now while retrieving the list from google spreadsheet. Hope this helps others who are using google spreadsheet.
adjust the height in the config of view......this might be a problem