O'Reilly logo

Dart 1 for Everyone by Chris Strom

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required