Bind store data with check tree in Sencha - extjs
I am working with check tree in sencha.
This is my store
Ext.define('crApp.store.modulesStore', {
extend: 'Ext.data.TreeStore',
proxy: {
type: 'memory'
},
model: 'crApp.model.MainTreeModel',
defaultRootProperty: "children",
root: {
expanded: true,
children: [{"text":"Dashboard","moduleId":"1","checked":true,"expanded":true,"children":[{"permission_id":"3","permission_name":"View","text":"Dashboard/View","moduleId":"1_3","checked":false,"leaf":true}]},{"text":"Master","moduleId":"2","checked":true,"expanded":true,"children":[{"text":"Facility","moduleId":"3","checked":true,"expanded":true,"children":[{"permission_id":"1","permission_name":"Create","text":"Facility/Create","moduleId":"3_1","checked":true,"leaf":true},{"permission_id":"2","permission_name":"Edit","text":"Facility/Edit","moduleId":"3_2","checked":true,"leaf":true},{"permission_id":"3","permission_name":"View","text":"Facility/View","moduleId":"3_3","checked":true,"leaf":true},{"permission_id":"4","permission_name":"Delete","text":"Facility/Delete","moduleId":"3_4","checked":true,"leaf":true},{"permission_id":"5","permission_name":"Allocation","text":"Facility/Allocation","moduleId":"3_5","checked":true,"leaf":true}]},{"text":"Marketing","moduleId":"4","checked":true,"expanded":true,"children":[{"permission_id":"1","permission_name":"Create","text":"Marketing/Create","moduleId":"4_1","checked":true,"leaf":true},{"permission_id":"2","permission_name":"Edit","text":"Marketing/Edit","moduleId":"4_2","checked":true,"leaf":true},{"permission_id":"3","permission_name":"View","text":"Marketing/View","moduleId":"4_3","checked":true,"leaf":true},{"permission_id":"4","permission_name":"Delete","text":"Marketing/Delete","moduleId":"4_4","checked":true,"leaf":true}]},{"text":"Department","moduleId":"5","checked":true,"expanded":true,"children":[{"permission_id":"1","permission_name":"Create","text":"Department/Create","moduleId":"5_1","checked":true,"leaf":true},{"permission_id":"2","permission_name":"Edit","text":"Department/Edit","moduleId":"5_2","checked":true,"leaf":true},{"permission_id":"3","permission_name":"View","text":"Department/View","moduleId":"5_3","checked":true,"leaf":true},{"permission_id":"4","permission_name":"Delete","text":"Department/Delete","moduleId":"5_4","checked":true,"leaf":true}]},{"text":"User","moduleId":"6","checked":true,"expanded":true,"children":[{"permission_id":"1","permission_name":"Create","text":"User/Create","moduleId":"6_1","checked":true,"leaf":true},{"permission_id":"2","permission_name":"Edit","text":"User/Edit","moduleId":"6_2","checked":true,"leaf":true},{"permission_id":"3","permission_name":"View","text":"User/View","moduleId":"6_3","checked":true,"leaf":true},{"permission_id":"4","permission_name":"Delete","text":"User/Delete","moduleId":"6_4","checked":true,"leaf":true}]},{"text":"Doctor","moduleId":"7","checked":true,"expanded":true,"children":[{"permission_id":"1","permission_name":"Create","text":"Doctor/Create","moduleId":"7_1","checked":true,"leaf":true},{"permission_id":"2","permission_name":"Edit","text":"Doctor/Edit","moduleId":"7_2","checked":true,"leaf":true},{"permission_id":"3","permission_name":"View","text":"Doctor/View","moduleId":"7_3","checked":true,"leaf":true},{"permission_id":"4","permission_name":"Delete","text":"Doctor/Delete","moduleId":"7_4","checked":true,"leaf":true}]}]}]
}
});
When I use the static data, its working perfectly. The data is coming from php url : http://192.168.1.100:8088/CRApp/yii/web/module/index
How can I set the ajax url in store, so that view can access the data dynamically.
I tried this:
proxy: {
type: 'ajax',
url: 'http://192.168.1.100:8088/CRApp/yii/web/module/tree',
reader: {
type: 'json',
expanded: true,
root: 'children'
}
},
but its not working properly. Any solution?
Ext.define('AppName.store.settings.permissions.GroupPermissionsStore', {
extend: 'Ext.data.TreeStore',
alias: 'store.groupPermissionsStore',
autoLoad: true,
storeId: 'groupPermissionsStore',
load : function (options) {
var successFunction = function (response) {
var grpStore = Ext.data.StoreManager.get('groupPermissionsStore');
var resp = Ext.JSON.decode(response.responseText);
grpStore.setRoot(test);
}
};
//Make ajax call with above function as success function
Ext.Ajax.request({
url: "http://192.168.1.100:8088/CRApp/yii/web/module/index",
cors: true,
async: calltype,
withCredentials: true,
//jsonData: dataPayload,
method: 'POST',
success: successFunction,
failure: function (err) {
//Call back if the ajax call fails
}
});
}
});
Key change for Tree Store is method "setRoot" rather and load data.
For "Ext.data.StoreManager.get('groupPermissionsStore')" to work, the store has be added to stores array in the application's "app.js" file.
Related
How to pass parameters to store in ExtJs
I am working on a display where I need to bind a combobox but I am unable to pass parameters. Below is my code please provide me the way to pass parameters. //store Ext.define('NetworkStore', { extend: 'Ext.data.Store', alias: 'NetworkStore', fields: ['Id', 'value'], storeId: 'NetworkStore', autoLoad: true, proxy: { type: 'ajax', useDefaultXhrHeader: false, actionMethods: { create: "POST", read: "GET", update: "POST", destroy: "POST" }, headers: { 'Content-Type': 'application/x-www-form-urlencode' }, limitParam: false, startParam: false, pageParam: false, extraParams: { Style: 1 }, url: 'url', reader: { type: 'json' } } }); xtype: 'combo', name: 'NetworkIDList', store: { type: 'NetworkStore', 'Style': 3 //parameter //params: { // tried this way as well but did not work for me. // Style: 3 //} } Note: Store is defined in a separate file.
The store should be passed like below format. store: { type: 'NetworkStore', proxy: { extraParams: { Style: 3 } } }
You can change the params as follows: let store = Ext.data.StoreManager.lookup('NetworkStore'), // params are part of the proxy, not the store proxy = store.getProxy(), // make sure you keep the other params newParams = Ext.apply(proxy.getExtraParams(), {style: '3'}); // set the params to the proxy proxy.setExtraParams(newParams); // do whatever you plan to do with the store store.load(); If you want to bind a value to store proxy params, take a look at this fiddle.
extjs store proxy api throws error does not recognize url when passing it as a variable
I have a store which fetches data from rest api. Turns up that if I try to pass the url as a variable it throws me the following error: [E] Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have not supplied it with a url. Here is the code: Ext.define('mystore', { extend: 'Ext.data.Store', alias: 'store.mystore', model: 'mymodel', restful: true, autoLoad: true, proxy: { type: 'ajax', headers: { 'Accept': '*/*', 'Cache-Control': 'no-cache', 'Content-Type': 'application/json', 'Authorization': localStorage.token }, extraParams: { sort: 'clave', 'filter[active]': true }, reader: { type: 'json', rootProperty: 'data', successProperty: 'success' }, writer: { type: 'json', writeAllFields: true, encode: true, rootProperty: 'data' }, actionMethods: { read: 'GET' }, api: { read: this.url, create: this.url, update: this.url, destroy: this.url }, autoSave: true }, constructor: function (config) { this.url = 'http://myurl...'; console.log('>>>>>>>>>>>>>>>>>>>>>>>>',this.url); this.callParent(arguments); this.initConfig(config); return this; } }); So far I've read the constructor should be deployed in the very first place, and then the component. The thing is I have no idea on how to solve this, so how can I get this working? What am I doing the wrong way?
You can't use this.url in Ext.define because this don't refer to created store. You need to set url in constructor or override the buildUrl method. Example : constructor : function(config) { config = config || {}; var me = this; this.url = '..'; config.proxy = { api: { read: this.url, create: this.url, update: this.url, destroy: this.url } }; this.callParent([config]); }
I've found a solution myself, after a lot of reading in web foros and testing :( The thing is to construct the store and then pass url attrib from the controller which calls the view, that is to say: component.store = Ext.create('mystore', { url: 'part of the url' }); like this, when the component loads it knows already the value of url. Hope this might help.
How to load store via POST request
I need to load store via cross domain POST request Ext.define('MyDesktop.desktop.store.DesktopShortcutStore', { extend: 'Ext.data.Store', requires: [ 'MyDesktop.desktop.model.DesktopShortcutModel' ], constructor: function(cfg) { var me = this; cfg = cfg || {}; me.callParent([Ext.apply({ storeId: 'DesktopShortcutStore', model: 'MyDesktop.desktop.model.DesktopShortcutModel', autoLoad: false, proxy: { type: 'ajax', url: getApiUrl() + 'account/desktop-shortcuts-list', cors: true, reader: { type: 'json', rootProperty: 'items', successProperty: 'success' }, paramsAsJson: true, actionMethods: { read: 'POST' }, extraParams: { fff: 'zzz' } } }, cfg)]); } }); But I have OPTIONS request with no "fff" param. Cross domain via jQuery is working correctly. Using Ext.Ajax.useDefaultXhrHeader = false; Ext.Ajax.cors = true; Is not helps. I cannot find any information in docs.
This should be the easiest solution. Loading a store directly via a proxy and POST might require a few overrides, not sure it's worth the effort (needs more investigation). var store = ...; Ext.Ajax.request({ method: 'POST', url: '...', withCredentials: true, // for identity cookies success: function(response) { // given json array is returned var data = Ext.decode(response.responseText); Ext.each(data, function(r) { store.add(r); }); } });
sencha touch: change extraParams when it loaded
I should load date from server dynamically, however, I have no idea how to change get parameters. Here is my code. I want to load store when the view is loaded, but the store get data from server when the app is loaded. I need to change param according to user's input data. [Store] Ext.define('APP.store.MyTestStore', { extend: 'Ext.data.Store', requires: [ 'APP.model.MyModel', ], config: { model: 'APP.model.MyModel', autoLoad: false, storeId : 'MyTestStore', proxy: { type: 'jsonp', method: 'GET', // extraParams: { // man: '', // }, url: 'http://test.com/api/test/', reader: { type: 'json', rootProperty: 'apiList' } } }, }); [VIEW] Ext.define('APP.view.MyListView', { extend: 'Ext.navigation.View', xtype: 'applistview', requires: [ 'Ext.Panel', 'APP.view.TopMenu', 'APP.view.TestListView', 'APP.config.Runtime', ], fullscreen: true, config: { navigationBar: false, fullscreen: true, items: [ { // Top Menu xtype: 'topmenu', }, { // Main Menu xtype: 'tlist', itemId: 'myList', store: 'MyTestStore', }, { // Bottom Banner xtype: 'bottombanner', }, ] }, initialize: function(){ var test = Ext.getStore('MyTestStore').load({extraParams:{man:'test'}}); console.log(test); } });
You can do this in two ways, App default extra param set to 123 Ext.define('APP.store.MyTestStore', { extend: 'Ext.data.Store', requires: [ 'APP.model.MyModel' ], config: { model: 'APP.model.MyModel', autoLoad: false, storeId : 'MyTestStore', proxy: { type: 'jsonp', method: 'GET', extraParams: { man: '123' }, url: 'http://test.com/api/test/', reader: { type: 'json', rootProperty: 'apiList' } } } }); When loading the store you can change extra param like the following way initialize: function(){ var test = Ext.getStore('MyTestStore').getProxy().getExtraParams().man= '567' Ext.getStore('MyTestStore').load(); console.log(test); } In Another way you can change proxy url without setting extra param initialize: function(){ var url = 'http://test.com/api/test/'; // your store proxy url var modified = url+'parameter you get from user' Ext.getStore('MyTestStore').getProxy().setUrl(modified); Ext.getStore('MyTestStore').load(); console.log(test); }
Accessing a store from another store - Sencha Touch
I'm trying to create a store and inside access the data from another store to construct the proxy url. Something like this: Ext.define('MyApp.store.Post',{ extend:'Ext.data.Store', config: { model: 'MyApp.model.Post', proxy: { type: 'ajax', url: 'http://mywebsite.com/get?userid=' + Ext.getStore('UserData').getAt(0).data.userid, reader: { type: 'json' } } } }); So, I'm basically trying to get the userid from another store to be able to construct the correct url. This doesn't work, I get: Uncaught TypeError: Object #<Object> has no method 'getStore' What is the correct way to do this? EDIT: Okay I put in a dummy URL and trying to change it with a listener, this is my store now: Ext.define('MyApp.store.Post',{ extend:'Ext.data.Store', config: { fields: [ 'title', 'link', 'author', 'contentSnippet', 'content' ], proxy: { type: 'jsonp', url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog', reader: { type: 'json', rootProperty: 'responseData.feed.entries' } }, listeners: [ { beforeload: function(){ console.log("store loaded"); //I DON'T SEE THIS IN CONSOLE return true; } } ] }, });
Basically you did nothing wrong, but the reason is sencha touch uses asynchronous loading and it seems that Ext.getStore() is not instantiated at the time your store is defined. Let's try this method instead: First, add a listener for beforeload event inside your store config: Ext.define('MyApp.store.Post',{ extend:'Ext.data.Store', config: { model: 'MyApp.model.Post', proxy: { type: 'ajax', //url: you don't even need to set url config here, simply ignore it reader: { type: 'json' } } }, listeners: [ { fn: 'setUrl', event: 'beforeload' } ] }); then declare a function like this, in the same file: setUrl: function(){ this.getProxy().setUrl(Ext.getStore('UserData').getAt(0).data.userid); return true; } This way, it's ensured to set the url for your store's proxy right before it's loaded. And basically at the time, all core methods are instantiated. Update: please try this with your Post store: Ext.define('MyApp.store.Post',{ extend:'Ext.data.Store', config: { //autoLoad: true, fields: ['title', 'link', 'author', 'contentSnippet', 'content'], proxy: { type: 'jsonp', url: 'dummy url', reader: { type: 'json', rootProperty: 'responseData.feed.entries' } }, }, initialize: function(){ console.log("loaded!"); this.getProxy().setUrl('https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog'); //return true; } }); After reading the source code of the pull-to-refresh plugin, I see that Sencha Touch use an Ext.data.Operation instead of Ext.data.Store.load() function. So you will have to put it into the initialize method instead.
Use Ext.data.StoreManager.lookup('UserData') to get the store instance. But in your case, I would use this somewhere, where you work with the userid: var postsStoreInstance = ...; postsStoreInstancegetProxy()._extraParams.userid = userid; It adds a query paremetry to the store's proxy url
The way you do it is the correct way. What is the code of your store definition? Make sure your store has storeId: 'UserData'. See this working example: Ext.define('User', { extend: 'Ext.data.Model', fields: [ {name: 'id'} ] }); Ext.create( 'Ext.data.Store', { model: 'User', storeId: 'UserData' }); Ext.define('MyApp.store.Post',{ extend:'Ext.data.Store', config: { model: 'MyApp.model.Post', proxy: { type: 'ajax', url: 'http://mywebsite.com/get?userid=' + Ext.getStore('UserData'), reader: { type: 'json' } } } });