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 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.