Memoizing deferreds
Suppose we have the following routes in a front-end app:
/addresses
/checkout
When a user visits /addresses, we pull down their addresses. When they visit /checkout we pull down their addresses, their cart, and their payment methods. The addresses are needed and fetched on both pages.
Here's a nice pattern to ensure the addresses are only fetched once:
class User extends Backbone.Model
addresses: ->
unless @dfdAddresses
addresses = new AddressesCollection
@dfdAddresses = addresses.fetch()
@dfdAddresses
Then in the controllers:
# For /addresses
user.addresses().then (addresses) =>
@render()
# For /checkout
$.when(
cart.fetch(),
user.addresses(),
user.paymentMethods()
).then =>
@render()
Because the deferred is memoized, the addresses are only fetched once. That saves a request when, for example, the checkout page can direct the user to /addresses to change their shipping address.
Written by Kamil Tusznio
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Backbone
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#