Vary Class Behavior with noSuchMethod()

We first met noSuchMethod in Chapter 7, Classes and Objects. Let’s try to put it to use as a means for switching the behavior of the save method in the Hipster MVC library. We already have it saving to a REST-like back end. Let’s get it saving to either REST-like storage or localStorage.

Recall that when HipsterModel invokes save, it sends a JSON representation of its attributes to the REST-like data store and establishes handlers for successful updates.

mvc_library/public/scripts/HipsterModel.dart
 
class​ HipsterModel {
 
// ...
 
save({callback}) {
 
var​ req = ​new​ HttpRequest();
 
req.onLoad.listen((event) {
 
attributes = JSON.decode(req.responseText);
 
_onSave.add(​this​);
 
if​ (callback ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.