Skip to content
This repository has been archived by the owner on Sep 5, 2018. It is now read-only.

couple of questions about usage #109

Closed
MurhafSousli opened this issue Mar 14, 2016 · 3 comments
Closed

couple of questions about usage #109

MurhafSousli opened this issue Mar 14, 2016 · 3 comments

Comments

@MurhafSousli
Copy link

Hi, first of all thank you for the great job you do in this library, I'm currently using only Collections to get everything I need from wordpress, but there are some things I want to confirm.

What I understood till now:

  • Load is for wp authentication.
  • Collections is used to fetch data from wp.
  • Models is used for Create/Update/Delete operations.
  • Utils has the utility functions like changing date format.. etc

because I want to get the most out of this great library, I have couple of questions:

  1. for example I get a collection of posts which have undesired date format, and I've noticed that there is a function in utils.js converts the date format but couldn't figure out if I can use it.
  2. I had to make my own SinglePost class to handle a post response while I think (but I'm not sure) that the library offers one. wp.api.models.Posts.defaults is this the class?
  3. Can I get a single post using Models ? if so please show me how to use
  4. Do I have to dispose the service wp.api.collections after I finish using it? before using this library I used to use Observable from rxjs 5 and they don't need to be disposed

you can check how I'm using the library in my service file here

@kadamwhite
Copy link
Contributor

@MurhafSousli I need to turn in for the night but I'll try to review this Monday or Tuesday my time—thanks for the questions! If you haven't heard from me in 3 days feel free to bump this issue.

Edit: I've been doing triage on my generic JS client and didn't realize what repo I was in; I thought this was a follow-up to WP-API/node-wpapi#149 ! — @adamsilverstein is going to be better able to help out with this than I :)

Too many Adams...

@adamsilverstein
Copy link

Hello @MurhafSousli,

Glad to hear you are using the library.

The main thing this library provides are Models and Collections matching all the endpoints available in the WP-API.

A Model represents a single item in the API and WordPress, for example a Post.

A Collection is a group of models, for example a collection of posts.

Util and Load are primarily for internal functions, I don't think you need to use them except to help develop the library.

You are right that collections are often read only (or can save new models, but not edit them) and models generally support all CRUD methods. You can check what each endpoint does by examining the API schema, and each Model/Collection also provides information about the available methods in the methods property, for example for the Posts collection:

Here are some answers to your questions:

  1. don't worry about util, that date stuff is for internal helpers
  2. Sounds like you may want parse: http://backbonejs.org/#Collection-parse
  3. make sure you read the docs on the repo or here http://v2.wp-api.org/extending/javascript-client/, here is an example of getting a post once you know its id:
var post = new wp.api.models.Post( { id: 1 } );
var postPromise = post.fetch();
postPromise.done( function( model ) { ...
    do something 
} );
  1. you shouldn't need to worry about disposing of the service, although I haven't looked at your exact use case

Let me know if you have further questions, good luck. Closing for now, re-open if you have additional questions.

@MurhafSousli
Copy link
Author

Thank you so much.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants