Skip to content

Fetching data

Aferz edited this page Feb 3, 2016 · 2 revisions

There are plenty of forms to fetch the result data.

.get()

Basic fetch, it returns the filtered data as array:

var data = [ 
    { 
        id: 1, 
        name: 'Isaac', 
        birthdate: '1643-01-04', 
        swag: 99,
        study_fields: [ 'Gravity', 'Light', 'Motion' ],
        hasPet: undefined
    }, 
    { 
        id: 2, 
        name: 'Michael', 
        year: '1791-09-22', 
        swag: 87,
        study_fields: [ 'Electromagnetism', 'Electrochemistry' ],
        hasPet: true
    },
    { 
        id: 3, 
        name: 'Erwin', 
        year: '1887-08-12', 
        swag: 93,
        study_fields: [ 'Quantum Mechanics' ],
        hasPet: null
    },
];

var result = Vivaz( data ).get();

// Returns the same array, because there is no filter :p

.first()

It returns the first element of the filtered data. It returns an object, not an array.

var result = Vivaz( data ).first();

// { id: 1, name: 'Isaac', ... }

.last()

It returns the last element of the filtered data. It returns an object, not an array.

var result = Vivaz( data ).last();

// { id: 3, name: 'Erwin', ... }

.count()

It returns filtered data count. Returns an integer.

var result = Vivaz( data ).count();

// 3

.paginate()

Paginate filtered data in the number of pages you wish. It returns a Paginator object. See Paginator documentation.

var result = Vivaz( data ).paginate( 2 );

// Paginator object

Note: for obvious reasons, you can't paginate a grouped result.

.collect()

It returns filtered data in a Collection object. See Collection documentation.

var result = Vivaz( data ).collect( 2 );

// Collection object

Note: for obvious reasons, you can't collect a grouped result.

.toModel()

It returns filtered data and injects it into your preferred model. It creates a new instance for every record and return an array of models.

Parameters:

  • modelConstructor: Your model constructor.
  • args: If your model needs to be intializated with parameters, pass them as array.
  • override: If your model has a property that matches another one of the given results, this flag determines if it has to be overriden or alert you throwing an exception. Optional. Default false.
function Physicist( arg1 )
{
    this.arg1 = arg1;
}

var result = Vivaz( data ).toModel( Physicist, [ args1 ], true );

// [ Physicist (Isaac), Physicist (Michael), Physicist (Erwin) ]

Note: for obvious reasons, you can't paginate a grouped result.