Sync Through Dependency Injection

We need a mechanism to inject a syncing behavior that can be shared between model and collection. Let’s create a HipsterSync class that holds our data syncing behavior. Ultimately, the various libraries that rely on HipsterSync will invoke a static method HipsterSync.send() to dispatch the CRUD operation. Before looking at that, however, we need a default behavior that can perform Ajax requests.

varying_the_behavior/public/scripts/HipsterSync.dart
 
library​ hipster_sync;
 
import​ ​'dart:html'​;
 
import​ ​'dart:convert'​;
 
class​ HipsterSync {
 
static​ _defaultSync(method, model, {options}) {
 
var​ req = ​new​ HttpRequest();
 
_attachCallbacks(req, options);
 
 
req.open(method, model.url);
 
 
// POST ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.