| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Lazy implementation of the child nodes of an element that does not request | 8 * Lazy implementation of the child nodes of an element that does not request |
| 9 * the actual child nodes of an element until strictly necessary greatly | 9 * the actual child nodes of an element until strictly necessary greatly |
| 10 * improving performance for the typical cases where it is not required. | 10 * improving performance for the typical cases where it is not required. |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 // Should use $dom_firstChild, Bug 8886. | 328 // Should use $dom_firstChild, Bug 8886. |
| 329 this.insertBefore(newNodes[0], refChild); | 329 this.insertBefore(newNodes[0], refChild); |
| 330 } | 330 } |
| 331 } else { | 331 } else { |
| 332 for (var node in newNodes) { | 332 for (var node in newNodes) { |
| 333 this.insertBefore(node, refChild); | 333 this.insertBefore(node, refChild); |
| 334 } | 334 } |
| 335 } | 335 } |
| 336 } | 336 } |
| 337 | 337 |
| 338 // Note that this may either be the locally set model or a cached value |
| 339 // of the inherited model. This is cached to minimize model change |
| 340 // notifications. |
| 341 var _model; |
| 342 bool _hasLocalModel; |
| 343 StreamController<Node> _modelChangedStream; |
| 344 |
| 345 /** |
| 346 * The data model which is inherited through the tree. |
| 347 * |
| 348 * Setting this will propagate the value to all descendant nodes. If the |
| 349 * model is not set on this node then it will be inherited from ancestor |
| 350 * nodes. |
| 351 * |
| 352 * Currently this does not support propagation through Shadow DOMs. |
| 353 * |
| 354 * [clearModel] must be used to remove the model property from this node |
| 355 * and have the model inherit from ancestor nodes. |
| 356 */ |
| 357 @Experimental |
| 358 get model { |
| 359 // If we have a change handler then we've cached the model locally. |
| 360 if (_modelChangedStream != null) { |
| 361 return _model; |
| 362 } |
| 363 // Otherwise start looking up the tree. |
| 364 for (var node = this; node != null; node = node.parentNode) { |
| 365 if (node._hasLocalModel == true) { |
| 366 return node._model; |
| 367 } |
| 368 } |
| 369 return null; |
| 370 } |
| 371 |
| 372 @Experimental |
| 373 void set model(value) { |
| 374 var changed = model != value; |
| 375 _model = value; |
| 376 _hasLocalModel = true; |
| 377 _ModelTreeObserver.initialize(); |
| 378 |
| 379 if (changed) { |
| 380 if (_modelChangedStream != null) { |
| 381 _modelChangedStream.add(this); |
| 382 } |
| 383 // Propagate new model to all descendants. |
| 384 _ModelTreeObserver.propagateModel(this, value, false); |
| 385 } |
| 386 } |
| 387 |
| 388 /** |
| 389 * Clears the locally set model and makes this model be inherited from parent |
| 390 * nodes. |
| 391 */ |
| 392 @Experimental |
| 393 void clearModel() { |
| 394 if (_hasLocalModel == true) { |
| 395 _hasLocalModel = false; |
| 396 |
| 397 // Propagate new model to all descendants. |
| 398 if (parentNode != null) { |
| 399 _ModelTreeObserver.propagateModel(this, parentNode.model, false); |
| 400 } else { |
| 401 _ModelTreeObserver.propagateModel(this, null, false); |
| 402 } |
| 403 } |
| 404 } |
| 405 |
| 406 /** |
| 407 * Get a stream of models, whenever the model changes. |
| 408 */ |
| 409 Stream<Node> get onModelChanged { |
| 410 if (_modelChangedStream == null) { |
| 411 // Ensure the model is cached locally to minimize change notifications. |
| 412 _model = model; |
| 413 _modelChangedStream = new StreamController.broadcast(); |
| 414 } |
| 415 return _modelChangedStream.stream; |
| 416 } |
| 417 |
| 338 $!MEMBERS | 418 $!MEMBERS |
| 339 } | 419 } |
| OLD | NEW |