Vue
HTML
<div id="app">
</div>
Vue
const Pusher = { on: console.log } // dont pay attention, just mocking the Pusher instance
const initPusher = function ({pusher, collection, key, events}, component) {
collection = component[collection]
console.log(collection, 1)
pusher.on(events.created, (record) => {
collection.push(record)
})
pusher.on(events.updated, (record) => {
let r = _.find(collection, {[key]: record[key]})
if(r){
_.extend(r, record)
}
})
}
const PusherMixin = {
methods: {
// You can add some common methods for all your
// components making use of this mixin
},
created () {
initPusher(Object.assign({}, {
pusher: Pusher,
collection: 'collection',
key: 'id',
events: {
created: 'book-created',
updated: 'book-updated'
}
}, this.pusherOptions), this)
}
}
new Vue({
el: "#app",
mixins: [PusherMixin],
data () {
return {
books: [],
pusherOptions: {
collection: 'books',
key: 'id',
events: {
created: 'book-created',
updated: 'book-updated'
}
}
}
}
})