Sencha ExtJS Store “findRecord” query examples

Here are a few short examples of how to find an object in a Sencha Store (ExtJS or Sencha Touch).

As a first example, imagine that you have a Store of users, and want to find a user with the first name of “Alvin”. Assuming that your user model has a field named firstName, your query would look like this:

var user = usersStore.findRecord('firstName', 'Alvin');

After this query you can use the user object just as though you had created it by hand. (The object returned is a Model instance.)

Another example

As another short example, I’m working on a project where I need to find the id field for a project, given the project’s name, and used this code to search my project store:

// get the projectId from the projectName
getProjectId: function(projectName) {
    var projectsStore = this.getProjectsStore();
    var project = projectsStore.findRecord('name', projectName);
    return project.get('id');
},

Each project has a name field, so I search by that field, get a project object, then return the value of the id field. This is a simple application where I’m very sure that I’ll find a valid project object, but in more robust code you may want to check to make sure the object returned from the store is valid.

FWIW, my Project model looks like this:

Ext.define('Focus.model.Project', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [
        { name: 'id' },
        { name: 'userId' },
        { name: 'name' },
        { name: 'displayOrder' },
        { name: 'dateCreated' }
    ]
});

Limits

The Sencha Store documentation details a few limits of the findRecord method:

  • Finds the first matching Record in the store.
  • When store is filtered, finds records only within filter.
  • If this store is buffered, this can ONLY find records which happen to be cached in the page cache.

Documentation

The Store documentation states that findRecord takes these parameters, where only the first two are required:

  • fieldName
  • value
  • startIndex
  • anyMatch
  • caseSensitive
  • exactMatch

See the Store documentation for more details.

Comments

Permalink

Today I stumbled upon the fact that given this store:

{ id: "0", name: "Alarm flag" },
{ id: "9", name: "Clock changed" },
{ id: "128", name: "Clock set backward" },
{ id: "64", name: "Clock set forward" },
{ id: "6", name: "Configuration changed" },
{ id: "130", name: "Customer read" },
{ id: "259", name: "DST change occurred" },
{ id: "16", name: "DST in effect" },
{ id: "129", name: "Failed probe attempt" },
...
{ id: "10", name: "Load control occurred" },
...
{ id: "1", name: "Overflow condition detected" },
{ id: "2", name: "Partial interval" }

the call
myStore.findRecord('id', '1');
returns
{ id: "128", name: "Clock set backward" }

Huh???
I took a closer read to the ExtJs doc for the method findRecord and found this explanation about the 2nd parameter 'value':
"Either a string that the field value should BEGIN WITH, or a RegExp to test against the field."

In other words, given the above store, each of the following calls ALL return the above record:
storeRecord = myStore.findRecord('id', '1');
storeRecord = myStore.findRecord('id', '12');
storeRecord = myStore.findRecord('id', '128');

So, my advice:
Don't write:
storeRecord = myStore.findRecord('id', '1');
but

indexInStore = myStore.findExact('id', '1');
storeRecord = indexInStore === -1 ? null : myStore.getAt(indexInStore);