Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. export default {
  2. properties: {
  3.  
  4. // Data ID needed by every element
  5. uid: {
  6. type: String,
  7. observer: '_initUid'
  8. },
  9.  
  10. // Data associated with this UID
  11. data: Object,
  12.  
  13. // Whether the element is editable
  14. editable: Boolean,
  15.  
  16. // Keep reference to internal observers
  17. _observers: Array
  18.  
  19. },
  20.  
  21. // Connect the element's data to Simpla
  22. _initUid(uid) {
  23. let observer,
  24. setToValue = ({ data }) => {
  25. this.data = data;
  26. };
  27.  
  28. if (!uid) {
  29. return;
  30. }
  31.  
  32. // Get initial data from Simpla
  33. Simpla.get(this.uid).then(setToValue);
  34.  
  35. // Observe any future changes to this UIDs data
  36. observer = Simpla.observe(this.uid, setToValue);
  37.  
  38. // Store it in _observers so we can remove it on detatch
  39. this._observers.push(observer);
  40. },
  41.  
  42. // Mirror element's editable prop to Simpla editable state
  43. _observeEditable() {
  44. let observer,
  45. setEditable = (editable) => this.editable = editable;
  46.  
  47. // Check initial editable state on attach
  48. setEditable(Simpla.getState('editable'));
  49.  
  50. // Observe any future changes to editable state
  51. observer = Simpla.observeState('editable', setEditable);
  52.  
  53. // Store it in _observers so we can remove it on detatch
  54. this._observers.push(observer);
  55. },
  56.  
  57. // Sync element's value to Simpla's buffer
  58. _syncToSimpla(href) {
  59. Simpla.set(this.uid, {
  60. type: this.is,
  61. data: href
  62. });
  63. },
  64.  
  65. attached() {
  66. this._observeEditable();
  67. },
  68.  
  69. detatched() {
  70. // Clean up observers
  71. this._observers.forEach((observer) => {
  72. observer.unobserve();
  73. });
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement