Что делает этот код?

324
16 января 2017, 21:59

Что попадает в переменную "s"? И если не сложно, то по шагам объяснить этот код (Ниже есть пояснения к коду, но они не достаточно ясные для меня).

usersAddedOrRemoved: function(changeRecord) {
if (changeRecord) {
  changeRecord.indexSplices.forEach(function(s) {
    s.removed.forEach(function(user) {
      console.log(user.name + ' was removed');
    });

Из документации Полимера:

Use an array mutation observer to call an observer function whenever an array item is added or deleted using Polymer's array mutation methods. Whenever the array is mutated, the observer receives a change record representing the mutation as a set of array splices.

Your observer method should accept a single argument. When your observer method is called, it receives a change record of the mutations that occurred on the array. Each change record provides the following property:

indexSplices. The set of changes that occurred to the array, in terms of array indexes. Each indexSplices record contains the following properties:

index. Position where the splice started. removed. Array of removed items. addedCount. Number of new items inserted at index. object: A reference to the array in question. type: The string literal 'splice'.

Example:

Polymer({
  is: 'x-custom',
  properties: {
    users: {
      type: Array,
      value: function() {
        return [];
      }
    }
  },
  observers: [
    'usersAddedOrRemoved(users.splices)'
  ],
  usersAddedOrRemoved: function(changeRecord) {
    if (changeRecord) {
      changeRecord.indexSplices.forEach(function(s) {
        s.removed.forEach(function(user) {
          console.log(user.name + ' was removed');
        });
        for (var i=0; i<s.addedCount; i++) {
          var index = s.index + i;
          var newUser = s.object[index];
          console.log('User ' + newUser.name + ' added at index ' + index);
        }
      }, this);
    }
  },
  ready: function() {
    this.push('users', {name: "Jack Aubrey"});
  },
});
READ ALSO
jQuery. Как передать элемент в setTimeout

jQuery. Как передать элемент в setTimeout

Подписываюсь на событие click для всех кнопок с классом btnНужно, чтобы при клике нажатая кнопка становилась неактивной, а через 3 секунды становилась...

307
Защита лайков от накрутки, localStorage

Защита лайков от накрутки, localStorage

Возникла небольшая проблемаНиже приведен скрипт, который обрабатывает клик на кнопку лайк и затем создает объект в localStorage + делает кнопку...

310