-
-
Save dobbbri/5b261598389e33d8a5aa88e884ab25ea to your computer and use it in GitHub Desktop.
[WIP] - Vue.js Crud Mixin, adds create/read/update/delete etc methods to your component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Crud Mixin | |
* Usage: Add this mixin to provide crud actions | |
*/ | |
var Crud = { | |
data: function() { | |
return { | |
crud: null, | |
items: [], | |
item: null, | |
} | |
}, | |
methods: { | |
setResource: function(collection){ | |
this.crud = this.$resource(collection+'/:id'); | |
}, | |
store: function(items){ | |
this.$http.post(this.resource, items, function(){ | |
this.$emit('items-stored', data); | |
}).error(function (data, status, request) { | |
}); | |
}, | |
create: function(item){ | |
this.crud.save(item, function (data, status, request) { | |
this.$emit('item-added', data); | |
}).error(function (data, status, request) { | |
// handle error | |
}); | |
}, | |
read: function(){ | |
this.crud.get(function (items, status, request) { | |
this.$emit('items-read', items); | |
}); | |
}, | |
show: function(id){ | |
// get item | |
this.crud.get({id: id}, function (item, status, request) { | |
this.$set('item', item) | |
}); | |
}, | |
update: function(id){ | |
this.crud.update({id: id}, {item: this.item}, function (data, status, request) { | |
// handle success | |
}).error(function (data, status, request) { | |
// handle error | |
}); | |
}, | |
delete: function(id){ | |
// delete item | |
this.crud.delete({id: 1}, function (data, status, request) { | |
// handle success | |
}).error(function (data, status, request) { | |
// handle error | |
}); | |
} | |
} | |
} | |
module.exports = Crud; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment