API Docs for: 0.0.2
Show:

dataSource.Collection Class

Data source collection

Constructor

dataSource.Collection

(
  • config
)

Parameters:

Example:

dataSource:{
    url:'data-source/grid.php',
    id:'myDataSource',
    paging:{
        size:12,
        remotePaging:false,
        cache:false,
        cacheTimeout:1000
    },
    searchConfig:{
        index:['capital', 'country']
    },
    listeners:{
        select:function (record) {
            console.log(record);
        }
    }
}

Methods

addRecord

(
  • record
)
Object

Add a record to data-source

Parameters:

Returns:

Object: record

ascending

() dataSource.Collection

Set sort order to ascending

Returns:

Example:

collection.by('country').ascending().sort();

by

(
  • column
)
dataSource.Collection

Set sorted by column

Parameters:

Returns:

Example:

collection.by('country').ascending().sort();

or

collection.by('country').sort();

deleteRecord

(
  • search
)

Delete a single record. Deletes first match when multiple matches found.

Parameters:

Example:

grid.getDataSource().deleteRecord({ country: 'Norway' });

Will delete first found record where country is equal to Norway. It will fire a delete event if a record is found and deleted.

deleteRecords

(
  • search
)

Delete records matching search,

Parameters:

Example:

grid.getDataSource().deleteRecords({ country: 'Norway' });

will delete all records from collection where country is equal to "Norway". A delete event will be fired for each deleted record.

descending

() dataSource.Collection

Set sort order to descending

Returns:

Example:

collection.by('country').descending().sort();

findRecord

(
  • search
)
Object | Undefined

Returns plain object for a record from search. To get a dataSource.Record object use getRecord

collection.find({ capital : 'Oslo' });

Parameters:

Returns:

Object | Undefined: record

findRecords

(
  • search
)
Array

Find specific records, example: var records = collection.findRecords({ country:'Norway'});

Parameters:

Returns:

Array: records

getById

(
  • id
)
Object

Return record by id or undefined if not found. Records are indexed by id. This method gives you quick access to a record by it's id. The method returns a reference to the actual record. You can use Object.clone(record) to create a copy of it in case you want to update the record but not make those changes to the collection.

Parameters:

Returns:

Object: record

Example:

var collection = new ludo.dataSource.Collection({
    url : 'get-countries.php',
    primaryKey:'country'
});
var record = collection.getById('Japan'); // Returns record for Japan if it exists.

You can also define multiple keys as id

     var collection = new ludo.dataSource.Collection({
        url : 'get-countries.php',
        primaryKey:['id', 'country']
     });
   var record = collection.getById({ id:1, country:'Japan' });

This is especially useful when you have a dataSource.TreeCollection where child nodes may have same numeric id as it's parent.

{ id:1, type:'country', title : 'Japan',
    children:[ { id:1, type:'city', title:'Tokyo }]

By setting primaryKey to ['id', 'type'] will make it possible to distinguish between countries and cities.

getCount

() Number

Returns 1) If search is specified: number of records in search result, or 2) number of records in entire collection.

Returns:

Number: count

getData

() Success:true, data:

Return data loaded from server

Returns:

Success:true, data:: object data from server, example:

getNextOf

(
  • record
)
Object

Returns next record of given record.

Parameters:

Returns:

Object: next record

getPageCount

() Number

Return number of pages

Returns:

getPageNumber

() Number

Return current page number

Returns:

Number: page

getPreviousOf

(
  • record
)
Object

Returns previous record of given record

Parameters:

Returns:

Object: previous record

getRecord

(
  • search
)
dataSource.Record | Undefined

Returns dataSource.Record object for a record. If you want to update a record, you should first get a reference to dataSource.Record and then call one of it's methods.

Parameters:

Returns:

dataSource.Record | Undefined:

Example:

     var collection = new ludo.dataSource.Collection({
        url : 'get-countries.php',
        primaryKey:'country'
     });
 collection.getRecord('Japan').set('capital', 'tokyo');

getSearcher

() dataSource.CollectionSearch

Returns a dataSource.CollectionSearch object which you can use to filter a collection.

getSelectedRecord

() Object | Undefined

Return selected record

Returns:

Object | Undefined: record

getSelectedRecords

() Array

Return selected records

Returns:

Array: records

getSortedBy

() String

Return current sorted by column

Returns:

String: column

getSortOrder

() String

Return current sort order (asc|desc)

Returns:

String: order

getSourceType

()

Return data-source type(HTML or JSON)

Returns:

string source type

hasData

() Boolean

Has data loaded from server

Returns:

isOnPage

(
  • pageNumber
)
Boolean

True if on given page

Parameters:

Returns:

lastPage

()

Go to last page

load

()

Reload data from server Components using this data-source will be automatically updated

Returns:

void

loadUrl

(
  • url
)

Load content from a specific url

Parameters:

next

() Object

Select next record. If no record is currently selected, first record will be selected

Returns:

Object: record

nextPage

()

When paging is enabled, go to next page fire nextPage event

previous

() Object

Select previous record. If no record is currently selected, first record will be selected

Returns:

Object: record

previousPage

()

When paging is enabled, go to previous page. fire previousPage event

remoteSearch

(
  • search
)

Executes a remote search for records with the given data

Parameters:

selectRecord

(
  • search
)
Object | Undefined

Select a specific record

Parameters:

Returns:

Object | Undefined: record

selectRecordIndex

(
  • index
)

Select a specific record by index

Parameters:

selectRecords

(
  • search
)
Array

Select a collection of records

Parameters:

Returns:

Array: records

selectTo

(
  • search
)

Select records from current selected record to record matching search,

Parameters:

Example:

collection.selectRecord({ country: 'Norway' });
collection.selectTo({country: 'Denmark'});
var selectedRecords = collection.getSelectedRecords();

sendRequest

(
  • service
  • arguments
  • data
)

Send a new request

Parameters:

sort

()

Resort data-source

Returns:

void

sortBy

(
  • column
  • order
)
dataSource.Collection

Sort by column and order

The second argument(order) is optional

Parameters:

Returns:

Example:

grid.getDataSource().sortBy('firstname', 'desc');

which also can be written as

grid.getDataSource().by('firstname').descending().sort();

toPage

(
  • pageNumber
)
Boolean

Go to a specific page

Parameters:

Returns:

Boolean: success

updateRecord

(
  • search
  • updates
)
dataSource.Record

Update a record

Parameters:

Returns:

Attributes

arguments

Array

Array of arguments to send to resource on server

Default: '' Here are some examples: Create a data source for server resource "Person", service name "load" and id : "1". You will then set these config properties:

Example:

     "resource": "Person",
     "service": "load",
     "arguments": [1]

autoload

Boolean

Load data from external source on creation

Default: true

Fires event autoloadChange

Fires when the value for the configuration attribute autoload is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

object postData

Remote postData sent with request, example: postData: { getUsers: 1 }

Fires event object postDataChange

Fires when the value for the configuration attribute object postData is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

paging

Object

Use paging, i.e. only load a number of records from the server

Fires event pagingChange

Fires when the value for the configuration attribute paging is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

Example:

paging:{
        size:10, // Number of rows per page
        remotePaging:true, // Load only records per page from server, i.e. new request per page
        cache : true, // Store pages in cache, i.e no request if data for page is in cache,
        cacheTimeout:30 // Optional time in second before cache is considered out of date, i.e. new server request
    }

primaryKey

String optional

Primary key for records

Default: "id"

resource

String

Name of resource to request on the server

Default: ''

searchConfig

Object

Configuration object for dataSource.CollectionSearch. This is the class which searchs and filters data in the collection.

Example:

searchConfig:{
    index:['city','country'],
    delay:.5
}

which makes the record keys/columns "city" and "country" searchable. It waits .5 seconds before the search is executed. This is useful when searching large collections and you want to delay the search until the user has finished entering into a search box.

selected

Object | String

Reference to record to select by default once data has been loaded

Default: undefined

service

String

Name of service to request on the server

Default: ''

shim

Object

Config of shim to show when content is being loaded form server. This config object supports two properties, "renderTo" and "txt". renderTo is optional and specifies where to render the shim. Default is inside body of parent view. "txt" specifies which text to display inside the shim. "txt" can be either a string or a function returning a string.

Example:

shim:{
        renderTo:ludo.get('myView').getBody(),
        txt : 'Loading content. Please wait'
}

renderTo is optional. Example where "txt" is defined as function:

shim:{
    "txt": function(){
        var val = ludo.get('searchField).getValue();
        return val.length ? 'Searching for ' + val : 'Searching';
    }
}

singleton

Boolean

Accept only one data-source of this type. You also need to specify the "type" property which will be used as key in the global SINGELTON cache By using singletons, you don't have to do multiple requests to the server

Fires event singletonChange

Fires when the value for the configuration attribute singleton is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

sortFn

Function

custom sort functions, which should return -1 if record a is smaller than record b and 1 if record b is larger than record a.

Default: {}

Example:

sortFn:{
        'population':{
            'asc' : function(a,b){
                return parseInt(a.population) < parseInt(b.population) ? -1 : 1
            },
            'desc' : function(a,b){
                return parseInt(a.population) > parseInt(b.population) ? -1 : 1
            }
        }
}

url

String optional

Remote url. If not set, global url will be used

Fires event urlChange

Fires when the value for the configuration attribute url is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

Events

add

Event fired when a record is added to the collection

Event Payload:

delete

Event fired when a record is deleted

Event Payload:

deselect

Event fired when a record is selected

Event Payload:

failure

Event fired when success parameter in response from server is false

Event Payload:

  • JSON Object

    response from server. Error message should be in the "message" property

  • this ludo.dataSource.JSON

firstPage

Event fired when moving to first page

lastPage

Event fired when moving to last page

nextPage

Event fired when moving to next page

notFirstPage

Event fired when moving to a different page than last page

notLastPage

Event fired when moving to a different page than last page

page

Event fired when moving to a page

Event Payload:

previousPage

Event fired when moving to previous page

select

Event fired when a record is selected

Event Payload:

Example:

...
listeners:{
    'select' : function(record){
        console.log(record);
    }
}

servererror

Server error event. Fired when the server didn't handle the request

Event Payload:

sort

Event fired when a data has been sorted, param example: { column:'country',order:'asc' }

Event Payload:

toPage

Event fired when moving to a specific page