Question

I'm creating a data store that will load the data from server. I'm wondering how I can pass parameters to the proxy.

var dataStore = new Ext.data.JsonStore({
proxy:'productSearch.php',
root:'products',
fields:['title', 'image', 'inStock', 'price', 'category', 'manufacturer']
});
Was it helpful?

Solution

I usually do it like this

var dataStore = new Ext.data.JsonStore({
  url: 'productSearch.php'
  root: 'products',
  baseParams: {  //here you can define params you want to be sent on each request from this store
    param1: 'value1',
    param2: 'value2'
  },
  fields: [...]
});

dataStore.load({
  params: {  //here you can define params on 'per request' basis
    param3: 'value3'
  }
});

I also prefer to define fields like this:

fields: [
  {name: 'title', mapping: 'title', type: 'string'},
  {name: 'image', mapping: 'image', type: 'string'},
  {name: 'inStock', mapping: 'inStock', type: 'bool'},
  {name: 'price', mapping: 'price', type: 'float'},
  {name: 'category', mapping: 'category', type: 'int'},
  {name: 'someDate', mapping: 'someDate', type: 'date', dateFormat: 'Y-m-d'},
]

Two things here:

  1. I assign the types, so that the store is loaded with correct datatypes. It will even convert string dates to JavaScript Date() objects.

  2. I use 'mapping' parameter, to tell which fields from JSON are to be matched to which fields in the store. If for whatever reason JSON format changes, I only need to make one change here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top