npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

premote

v0.1.0-beta.1

Published

wrap javascript remote actions in a promise api

Downloads

355

Readme

Premote

Javascript Remoting in Visualforce has an ugly api. Premote fixes this by letting you wrap your Remote Action calls in a promise based on Q.

Requirements

To use Premote, you need to be using Q.

Installing

  • Include Q on your page in a <script> tag
  • Include Premote on your page using a <script> tag

Usage

You simply use Premote's wrap() function to turn any Remote action function into function that returns a Q promise. Here is the basic usage:

var getAccount = Premote.wrap('MyController.getAccount');

getAccount(accountId)
  .then(function(account) {
    console.log('got the account: ' + account.Name);
  })
  .fail(function(error){
    console.error(error.message);
  });

Here is the example controller and Apex method we are calling:

global class MyController {

  @RemoteAction
  global static Account getAccount(String accountId) {
    List<Account> accs = [SELECT Id, Name, Industry from Account WHERE Id = :accountId LIMIT 1];
    if(accs.size() == 1) {
        return accs.get(0);
    }
    return null;
  }

}

You can also wrap your Remote Action function calls with Visualforce Remoting options.

var getAccount = Premote.wrap('MyController.getAccount', { escape: true, timeout: 10000 });

Remember that Visualforce has tags to output the fully-qualified remote action name (including namespace):

var getAccount = Premote.wrap('{!$RemoteAction.MyController.getAccount}');

Advanced Examples

Say you have two Remote Actions, one to create an Account and one to create a Contact and you need to create an Account with multiple Contacts associated to it.

Doing this requires that the Account is created first so you can get the AccountId. Only after the Account is created can you create the Contacts because you need the AccountId to be able to associate them.

Since our createContact function only creates one contact at a time, it would be nice if we could create the Account, then create the Contacts in parallel http requests, then resolve a single promise back to the user.

Here is a how you would compose a single function to do all of that:

var createAccount = Premote.wrap('MyController.createAccount');
var createContact = Premote.wrap('MyController.createContact');

function createAccountWithContacts(account, contacts) {
  // create the account first
  return createAccount(account)
    // create the contacts in parallel
    .then(function(acc) { 
      var promises = [];
      contacts.forEach(function(con) {
        con.AccountId = acc.Id;
        promises.push(createContact(con));
      });
      return Q.all(promises);
    });
};

Now we can use it:

var account = { Name: 'ABC Company', Industry: 'Manufacturing'};

var contacts = [
  { FirstName: 'Johnny', LastName: 'Dough', Email: '[email protected]' },
  { FirstName: 'Bobby', LastName: 'Tester', Email: '[email protected]' }
];

createAccountWithContacts(account, contacts)
  .then(function(){
    console.log('account and contacts created');
  })
  .fail(function(){
    console.error('an error occurred');
  });

Benefits

The main benefit is that Premote allows you to use true promises to manage asynchronous flow control when invoking Javascript Remote Actions. If you aren't familiar with promises, I highly recommend you read the documentation from the Q README.