The following code calls Parse. And uses its API to create two functions: post.create
and post.find
. As the names imply, one is for creating posts and the other for fetching them.
Example:
import Parse from 'parse'
Parse.initialize('APP_ID', 'CLIENT_KEY')
const post = {}
post.create = json => {
const Post = Parse.Object.extend('Post')
const post = new Post()
post.save(json).then(object => {
console.log('yay! it worked', object)
})
}
post.find = () => {
const query = new Parse.Query(Post)
let arr = []
query.find({
success: function (results) {
results.forEach(function (result) {
arr.push(result.toJSON())
})
},
error: function (error) {
alert('Error: ' + error.code + ' ' + error.message)
}
})
return arr
}
export default post
As you can see, const Post = Parse.Object.extend('Post')
is being written twice. I could just declare it once at the top of the file, on the other hand, they would be farther from the place they are used.
What's the conventional option here?