Events

Swick uses its own event system, largely inspired by Backbone.JS. Every class of Swick extends base Swick.Eventer class (and you can extend it too, of course), so all methods below available everywhere.

The main difference between Backbone and Swick events is that Swick events are hierarchical. For example, an event named 'change:user.photo.url' is actually divided into four parts: change, user, photo and url. You can use :, . and / as delimiters: they all are equal.

What's more important is that you can use wildcards when adding your listeners. For example, 'change:user.*' will listen to any change which occurs in user field (or within any of its children). And using '?:user.name' allows capturing all event types which relate only to user.name field. You can use # instead of * and + instead of ?, making syntax similar to the one MQTT uses.

As you might have guessed, Swick uses this system to notify your components about data updates. But how do you trigger those events when the whole object has changed? Do you need to manually call trigger for each of the fields (and for each field of nested objects)?

For this scenario Swick allows specifying not just a single event to trigger, but a whole tree of them. It looks just like a regular JavaScript object:

app.trigger({
  change: {
    user: {
      name: true,
      photo: {
        url: true,
      }
    }
  },
  add: {
    auth: true,
  },
  like: true,
}, payload);

This will cause Swick to call all listeners for events like change:*, ?:user.name, add/auth and like. The only downside is that you can pass only one payload to all those listeners. For example, when Swick notifies about data updates, it passes the whole object, not just the fields that were changed.