API Module

Allows making requests to REST API, not using third party libraries.  Read how to make a request in the  user guide.
 
Before sending request, you need to create it. The request starts with the method you want to call:
    var friendsList = Dudu.api('friends.list');

Then specify the method parameters:
friendsList.params({
	count: 20
});

And don't forget about the mask. Otherwise, you will get almost empty response:
friendsList.mask({
    fullname: 1,
    gender: 1
});

The request is actually sent after the .get() method is called. In response "the promised resource" is returned being an object with which you can work as if the request has already been executed (see Resource).
friendsList.get()
	// this instanceof Resource === true
    .done(function (result) {
        return result.friends;
    })
    .save()
    .filter(function (friend) {
        // Find all female friends with a name starting with "Ba"
        return friend.gender === Dudu.GENDER_FEMALE && friend.fullname.match(/^Ba/);
    })
    .each(function (female_friend) {
        // Reverse position of first and last names
        female_friend.fullname = female_friend.fullname.split(' ').reverse().join(' ');
        return female_friend;
    })
    .map(function (female_friend) {
        // Make array with only names of friends
        return female_friend.fullname;
    })
    .done(function (female_friend) {
        console.log('Filtered friends: ', female_friend);
        // $.tmpl('friends-list', female_friend).appendTo('#update');
    })
    .restore(function (friends) {
        console.log('Restore: ', friends)
        //--> Will return list of all friends
    });

After sending request, created request object can be used for the next request, i.e. you can change the request parameters and send it. All the handlers, masks and parameters previously set are saved.
friendsList1 = friendsList.get();
friendsList2 = friendsList.params({
        count: 20,
        offset: 40
    })
    .get();

Dudu.api(methodName)

Creates request to REST API with the specified method. Actually, a Request-type instance is created.

Parameters


  • methodName {String} — method name (see the list of methods)

Result

Request

Example

Dudu.api('friends.list')
    // this instanceof Request === true
    .mask({
        fullname: 1,
        gender: 1
    })
    .params({
        count: 100
    })
    .on('error', function () {
        console.log('Error', arguments);
    })
    .get()
        // this instanceof Resource === true
    .done(function (result) {
        return result.friends;
    })
    .save()
    .filter(function (friend) {
        // Find all female friends with a name starting with "Ba"
        return friend.gender === Dudu.GENDER_FEMALE && friend.fullname.match(/^Ba/);
    })
    .each(function (female_friend) {
        // Reverse position of first and last names
        female_friend.fullname = female_friend.fullname.split(' ').reverse().join(' ');
        return female_friend;
    })
    .map(function (female_friend) {
        // Make array with only names of friends
        return female_friend.fullname;
    })
    .done(function (female_friend) {
    console.log('Filtered friends: ', female_friend);
        // $.tmpl('friends-list', female_friend).appendTo('#update');
    })
    .restore(function (friends) {
    console.log('Restore: ', friends)
        //--> Will return list of all friends
    });