From 85a88e67321fcdf782afa01cd6fd2e787778690d Mon Sep 17 00:00:00 2001 From: Pau Ramon Revilla Date: Fri, 10 Mar 2017 17:03:25 +0100 Subject: [PATCH] Fixed progress and added `toArray` --- package.json | 2 +- src/Collection.js | 8 ++++++++ src/Model.js | 11 ++++++++++- src/Request.js | 4 ++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f5b69cb..b278548 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "2.0.3", + "version": "2.0.4", "description": "REST conventions for mobx.", "repository": { "type": "git", diff --git a/src/Collection.js b/src/Collection.js index b5597ce..c64cc01 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -49,6 +49,14 @@ export default class Collection { 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 diff --git a/src/Model.js b/src/Model.js index 2b0f6c0..7689f67 100644 --- a/src/Model.js +++ b/src/Model.js @@ -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) diff --git a/src/Request.js b/src/Request.js index b5a5fca..8fda77e 100644 --- a/src/Request.js +++ b/src/Request.js @@ -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 } }