moldy-ajax-adapter

1.3.0 • Public • Published

TOC

model-ajax-adapter

Tell the Moldy to use the ajax adapter.

// Moldy.use( require('moldy-ajax-adapter') );

create

should create by a property.

var personMoldy = Moldy.extend( 'person', {
    key: 'hash',
    baseUrl: 'http://localhost:3000/api',
    properties: {
        name: '',
        age: ''
    }
} ).create();
personMoldy.name = 'David';
personMoldy.$save( function ( _error ) {
    personMoldy.name.should.eql( 'David' );
    _done( _error );
} );

findOne

should findOne by a property.

var personMoldy = Moldy.extend( 'person', {
    key: 'guid',
    baseUrl: 'http://localhost:3000/api',
    properties: {
        name: '',
        age: ''
    }
} );
personMoldy.$findOne( {
    guid: '5f55821f-3a28-45c3-b91d-7df927a863d8'
}, function ( _error, _person ) {
    _person.name.should.eql( 'Bennett Sanchez' );
    _done( _error );
} );

find

should find.

var personMoldy = Moldy.extend( 'person', {
    key: 'guid',
    baseUrl: 'http://localhost:3000/api',
    properties: {
        name: '',
        age: ''
    }
} );
personMoldy.$find( function ( _error, _people ) {
    _people.should.be.an.Array;
    _people.should.have.a.lengthOf( 3 );
    _people.forEach( function ( _person ) {
        _person.should.be.a.Moldy;
        _person.should.have.a.property( 'guid' );
        _person.should.have.a.property( 'name' );
        _person.should.have.a.property( 'age' );
        Object.keys( _person.$json() ).should.have.a.lengthOf( 3 );
    } );
    _done( _error );
} );

save

should save a model.

var personMoldy = Moldy.extend( 'person', {
    key: 'guid',
    baseUrl: 'http://localhost:3000/api',
    properties: {
        name: '',
        age: ''
    }
} );
personMoldy.$findOne( {
    guid: '5f55821f-3a28-45c3-b91d-7df927a863d8'
}, function ( _error, _person ) {
    if ( _error ) {
        return _done( _error );
    }
    _person.name = 'Mr Bennett Sanchez';
    _person.$save( _done );
} );

destroy

should destroy a model.

var personMoldy = Moldy.extend( 'person', {
    key: 'guid',
    baseUrl: 'http://localhost:3000/api',
    properties: {
        name: '',
        age: ''
    }
} );
personMoldy.$findOne( {
    guid: '5f55821f-3a28-45c3-b91d-7df927a863d8'
}, function ( _error, _person ) {
    if ( _error ) {
        return _done( _error );
    }
    _person.$destroy( _done );
} );

Readme

Keywords

none

Package Sidebar

Install

npm i moldy-ajax-adapter

Weekly Downloads

8

Version

1.3.0

License

MIT

Last publish

Collaborators

  • davidtsuji
  • dervalp
  • garrows