Tuesday, June 7, 2016

Defered object usage in sharepoint

function getCachedSearches() {
    var dfd = $.Deferred(function () {
        var list = context
                .get_web()
                .get_lists()
                .getByTitle('MyList');

        cached_searches = list.getItems('');

        context.load(cached_searches);

        context.executeQueryAsync(
           function () {
               dfd.resolve();
           },
           function (sender, args) {
               dfd.reject(args.get_message());
           }
        );
    });
    return dfd.promise();
}

function addCachedSearch(phrase) {
    var dfd = $.Deferred(function () {
        var list = context
                .get_web()
                .get_lists()
                .getByTitle('CachedSearches');

        var create_item = new SP.ListItemCreationInformation();
        var list_item = list.addItem(create_item);

        list_item.set_item('Title', phrase);

        list_item.update();

        context.load(list_item);

        context.executeQueryAsync(
           function () {
               dfd.resolve();
           },
           function (sender, args) {
               dfd.reject(args.get_message());
           }
        );
    });
    return dfd.promise();
}

function doSearch() {
    addCachedSearch($('#phrase').val())
        .then(getCachedSearches);
}

No comments:

Post a Comment