VueWild
Vue.js 1 & 2 binding for Wilddog
Installation
- If included as global
<script>
: will install automatically if globalVue
is present.
<!-- Vue --><!-- Wilddog --><!-- VueWild -->
- In module environments, e.g CommonJS:
npm install vue wilddog vuewild --save
var Vue =var VueWild =var wilddog =// explicit installation required in module environmentsVue
Usage
var wilddogApp = wilddogvar db = wilddogAppvar vm =el: '#demo'wilddog:// simple syntax, bind as an array by defaultanArray: db// can also bind to a query// anArray: db.ref('url/to/my/collection').limitToLast(25)// full syntaxanObject:source: db// optionally bind as an objectasObject: true// optionally provide the cancelCallback{}// this is called once the data has been retrieved from wilddog{}
If you need to access properties from the Vue instance, use the function syntax:
var vm = el: '#demo' { return anArray: db }
⚠️: This function will get executed only once. If you want to have automatic rebind (pretty much like a computed property) use a $watch
and call $unbind
and then $bindAsArray
About the cancelCallback:
An optional callback that will be notified if your event subscription is ever canceled because your client does not have permission to read this data (or it had permission but has now lost it). This callback will be passed an
Error
object indicating why the failure occurred.
{{ anObject }}{{ item.text }}
The above will bind the Vue instance's anObject
and anArray
to the respective Wilddog data sources. In addition, the instance also gets the $wilddogRefs
property, which holds the refs for each binding:
// add an item to the arrayvm$wilddogRefsanArray
Alternatively, you can also manually bind to a Wilddog ref with the $bindAsObject
or $bindAsArray
instance methods:
vmvm// You can also pass cancelCallback and readyCallback callbacks functions as// a third and fourth arguments. Any of them can be omitted by passing nullvm// References are unbound when the component is destroyed but you can manually unbind a reference// if neededvm
To save user-input to your Wilddog database, simply push the data onto this.$wilddogRefs.items
(instead of this.items
) within a Vue method to automatically sync with Wilddog.
For example, in your template you could add something simple like
Add item
And within your Vue component
{ return item: '' } wilddog: items: db methods: { this$wilddogRefsitems }
Data Normalization
Array Bindings
Each record in the bound array will contain a .key
property which specifies the key where the record is stored. So if you have data at /items/-Jtjl482BaXBCI7brMT8/
, the record for that data will have a .key
of "-Jtjl482BaXBCI7brMT8"
.
If an individual record's value in the database is a primitive (boolean, string, or number), the value will be stored in the .value
property. If the individual record's value is an object, each of the object's properties will be stored as properties of the bound record. As an example, let's assume the /items/
node you bind to contains the following data:
The resulting bound array stored in vm.items
will be:
To delete or update an item you can use the .key
property of a given object. But keep in mind you have to remove the .key
attribute of the updated object:
// Vue instance methods{this$wilddogRefsitems}{// create a copy of the itemconst copy = ...item// remove the .key attributedelete copy'.key'this$wilddogRefsitems}
You can check the full example at examples/todo-app.
Contributing
Clone the repo, then:
$ npm install # install dependencies $ npm test # run test suite with coverage report $ npm run dev # watch and build dist/vuewild.js $ npm run build # build dist/vuewild.js and vuewild.min.js