Skip to content

Instantly share code, notes, and snippets.

@tomenden
Last active October 21, 2021 13:02
Show Gist options
  • Save tomenden/e456e27b9a8d65c12482e8c0b9e80357 to your computer and use it in GitHub Desktop.
Save tomenden/e456e27b9a8d65c12482e8c0b9e80357 to your computer and use it in GitHub Desktop.
import {makeAutoObservable, autorun} from 'mobx'
import * as uuid from 'uuid'
class RepeaterStore {
constructor(){
this.items = []
}
}
function connectRepeater({selector = '#repeater1', initialData, renderItem}) {
const store = makeAutoObservable(new RepeaterStore())
store.items = initialData.map(i => ({...i, _id: uuid.v4()}))
$w(selector).onItemReady((item, itemData) => {
autorun(() => renderItem(item, store.items.find((item) => item._id === itemData._id)))
})
autorun(() => {
$w(selector).data = store.items.slice()}
)
return {
setData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment