Skip to content

Commit

Permalink
Fixed progress and added toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Mar 10, 2017
1 parent bd08ccf commit 85a88e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "2.0.3",
"version": "2.0.4",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export default class Collection<T: Model> {
return toJS(this.models)
}

/**
* Returns a shallow array representation
* of the collection
*/
toArray () {
return this.models.slice()
}

/**
* Questions whether the request exists
* and matches a certain label
Expand Down
11 changes: 10 additions & 1 deletion src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,19 @@ export default class Model {
data = Object.assign({}, originalAttributes, attributes)
}

const onProgress = debounce(function onProgress (progress) {
if (optimistic && this.request) {
this.request.progress = progress
}
}, 300)

const { promise, abort } = apiClient().put(
this.url(),
data,
{ method: patch ? 'PATCH' : 'PUT' }
{
method: patch ? 'PATCH' : 'PUT',
onProgress
}
)

if (optimistic) this.set(newAttributes)
Expand Down
4 changes: 2 additions & 2 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type { Label } from './types'
export default class Request {
label: Label
abort: () => void
progress: number
@observable progress: number = 0

constructor (label: Label, abort: () => void, progress: number) {
this.label = label
this.abort = abort
this.progress = observable(progress)
this.progress = progress
}
}

0 comments on commit 85a88e6

Please sign in to comment.