|
7 | 7 | * |
8 | 8 | * Internally, the $firebase object depends on this class to provide 5 $$ methods, which it invokes |
9 | 9 | * to notify the array whenever a change has been made at the server: |
10 | | - * $$added - called whenever a child_added event occurs, returns the new record, or null to cancel |
11 | | - * $$updated - called whenever a child_changed event occurs, returns true if updates were applied |
12 | | - * $$moved - called whenever a child_moved event occurs, returns true if move should be applied |
13 | | - * $$removed - called whenever a child_removed event occurs, returns true if remove should be applied |
| 10 | + * $$added - called whenever a child_added event occurs |
| 11 | + * $$updated - called whenever a child_changed event occurs |
| 12 | + * $$moved - called whenever a child_moved event occurs |
| 13 | + * $$removed - called whenever a child_removed event occurs |
14 | 14 | * $$error - called when listeners are canceled due to a security error |
15 | 15 | * $$process - called immediately after $$added/$$updated/$$moved/$$removed |
16 | 16 | * (assuming that these methods do not abort by returning false or null) |
|
286 | 286 | }, |
287 | 287 |
|
288 | 288 | /** |
289 | | - * Called by $firebase to inform the array when a new item has been added at the server. |
290 | | - * This method must exist on any array factory used by $firebase. |
| 289 | + * Called to inform the array when a new item has been added at the server. |
| 290 | + * This method should return the record (an object) that will be passed into $$process |
| 291 | + * along with the add event. Alternately, the record will be skipped if this method returns |
| 292 | + * a falsey value. |
291 | 293 | * |
292 | 294 | * @param {object} snap a Firebase snapshot |
293 | 295 | * @param {string} prevChild |
|
313 | 315 | }, |
314 | 316 |
|
315 | 317 | /** |
316 | | - * Called by $firebase whenever an item is removed at the server. |
| 318 | + * Called whenever an item is removed at the server. |
317 | 319 | * This method does not physically remove the objects, but instead |
318 | 320 | * returns a boolean indicating whether it should be removed (and |
319 | 321 | * taking any other desired actions before the remove completes). |
320 | 322 | * |
321 | 323 | * @param {object} snap a Firebase snapshot |
322 | 324 | * @return {boolean} true if item should be removed |
| 325 | + * @protected |
323 | 326 | */ |
324 | 327 | $$removed: function(snap) { |
325 | 328 | return this.$indexFor($firebaseUtils.getKey(snap)) > -1; |
326 | 329 | }, |
327 | 330 |
|
328 | 331 | /** |
329 | | - * Called by $firebase whenever an item is changed at the server. |
| 332 | + * Called whenever an item is changed at the server. |
330 | 333 | * This method should apply the changes, including changes to data |
331 | 334 | * and to $priority, and then return true if any changes were made. |
332 | 335 | * |
| 336 | + * If this method returns false, then $$process will not be invoked, |
| 337 | + * which means that $$notify will not take place and no $watch events |
| 338 | + * will be triggered. |
| 339 | + * |
333 | 340 | * @param {object} snap a Firebase snapshot |
334 | 341 | * @return {boolean} true if any data changed |
| 342 | + * @protected |
335 | 343 | */ |
336 | 344 | $$updated: function(snap) { |
337 | 345 | var changed = false; |
|
345 | 353 | }, |
346 | 354 |
|
347 | 355 | /** |
348 | | - * Called by $firebase whenever an item changes order (moves) on the server. |
| 356 | + * Called whenever an item changes order (moves) on the server. |
349 | 357 | * This method should set $priority to the updated value and return true if |
350 | 358 | * the record should actually be moved. It should not actually apply the move |
351 | 359 | * operation. |
352 | 360 | * |
| 361 | + * If this method returns false, then the record will not be moved in the array |
| 362 | + * and no $watch listeners will be notified. (When true, $$process is invoked |
| 363 | + * which invokes $$notify) |
| 364 | + * |
353 | 365 | * @param {object} snap a Firebase snapshot |
354 | 366 | * @param {string} prevChild |
| 367 | + * @protected |
355 | 368 | */ |
356 | 369 | $$moved: function(snap/*, prevChild*/) { |
357 | 370 | var rec = this.$getRecord($firebaseUtils.getKey(snap)); |
|
365 | 378 | /** |
366 | 379 | * Called whenever a security error or other problem causes the listeners to become |
367 | 380 | * invalid. This is generally an unrecoverable error. |
| 381 | + * |
368 | 382 | * @param {Object} err which will have a `code` property and possibly a `message` |
| 383 | + * @protected |
369 | 384 | */ |
370 | 385 | $$error: function(err) { |
371 | 386 | $log.error(err); |
|
376 | 391 | * Returns ID for a given record |
377 | 392 | * @param {object} rec |
378 | 393 | * @returns {string||null} |
379 | | - * @private |
| 394 | + * @protected |
380 | 395 | */ |
381 | 396 | $$getKey: function(rec) { |
382 | 397 | return angular.isObject(rec)? rec.$id : null; |
383 | 398 | }, |
384 | 399 |
|
385 | 400 | /** |
386 | 401 | * Handles placement of recs in the array, sending notifications, |
387 | | - * and other internals. Called by the $firebase synchronization process |
388 | | - * after $$added, $$updated, $$moved, and $$removed. |
| 402 | + * and other internals. Called by the synchronization process |
| 403 | + * after $$added, $$updated, $$moved, and $$removed return a truthy value. |
389 | 404 | * |
390 | 405 | * @param {string} event one of child_added, child_removed, child_moved, or child_changed |
391 | 406 | * @param {object} rec |
392 | 407 | * @param {string} [prevChild] |
393 | | - * @private |
| 408 | + * @protected |
394 | 409 | */ |
395 | 410 | $$process: function(event, rec, prevChild) { |
396 | 411 | var key = this.$$getKey(rec); |
|
426 | 441 | }, |
427 | 442 |
|
428 | 443 | /** |
429 | | - * Used to trigger notifications for listeners registered using $watch |
| 444 | + * Used to trigger notifications for listeners registered using $watch. This method is |
| 445 | + * typically invoked internally by the $$process method. |
430 | 446 | * |
431 | 447 | * @param {string} event |
432 | 448 | * @param {string} key |
433 | 449 | * @param {string} [prevChild] |
434 | | - * @private |
| 450 | + * @protected |
435 | 451 | */ |
436 | 452 | $$notify: function(event, key, prevChild) { |
437 | 453 | var eventData = {event: event, key: key}; |
|
522 | 538 | }; |
523 | 539 |
|
524 | 540 | /** |
525 | | - * This method allows FirebaseArray to be copied into a new factory. Methods passed into this |
| 541 | + * This method allows FirebaseArray to be inherited by child classes. Methods passed into this |
526 | 542 | * function will be added onto the array's prototype. They can override existing methods as |
527 | 543 | * well. |
528 | 544 | * |
|
531 | 547 | * FirebaseArray. It's also possible to do both, passing a class to inherit and additional |
532 | 548 | * methods to add onto the prototype. |
533 | 549 | * |
534 | | - * Once a factory is obtained by this method, it can be passed into $firebase as the |
535 | | - * `arrayFactory` parameter: |
536 | | - * <pre><code> |
537 | | - * var MyFactory = $FirebaseArray.$extendFactory({ |
| 550 | + * <pre><code> |
| 551 | + * var ExtendedArray = $FirebaseArray.$extend({ |
538 | 552 | * // add a method onto the prototype that sums all items in the array |
539 | 553 | * getSum: function() { |
540 | 554 | * var ct = 0; |
|
544 | 558 | * }); |
545 | 559 | * |
546 | 560 | * // use our new factory in place of $FirebaseArray |
547 | | - * var list = $firebase(ref, {arrayFactory: MyFactory}).$asArray(); |
| 561 | + * var list = new ExtendedArray(ref); |
548 | 562 | * </code></pre> |
549 | 563 | * |
550 | 564 | * @param {Function} [ChildClass] a child class which should inherit FirebaseArray |
551 | 565 | * @param {Object} [methods] a list of functions to add onto the prototype |
552 | | - * @returns {Function} a new factory suitable for use with $firebase |
| 566 | + * @returns {Function} a child class suitable for use with $firebase (this will be ChildClass if provided) |
| 567 | + * @static |
553 | 568 | */ |
554 | 569 | FirebaseArray.$extend = function(ChildClass, methods) { |
555 | 570 | if( arguments.length === 1 && angular.isObject(ChildClass) ) { |
|
0 commit comments