Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/179422

Change subject: statementview: Removed _statement variable
......................................................................

statementview: Removed _statement variable

- _statement was redundant to options.value.
- As to our common pattern, value() now returns current value instead of 
original/stored value.

Change-Id: If0cb699ec36f06177322ba313f2dfae853f06037
---
M lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
1 file changed, 42 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/22/179422/1

diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
index 6c47026..499ff88 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
@@ -31,7 +31,8 @@
  *
  * @param {Object} options
  * @param {wikibase.datamodel.Statement|null} [options.value=null]
- *        The `Statement` displayed by the view. May be set initially only.
+ *        The `Statement` displayed by the view. May be set initially only and 
gets updated
+ *        automatically if changes to the `Statement` are saved.
  *        If `null`, the view will be switched to edit mode initially.
  * @param {wikibase.entityChangers.ClaimsChanger} options.claimsChanger
  *        Required to store the view's `Statement`.
@@ -108,7 +109,7 @@
                template: 'wikibase-statementview',
                templateParams: [
                        function() { // GUID
-                               return ( this._statement && 
this._statement.getClaim().getGuid() ) || 'new';
+                               return ( this.options.value && 
this.options.value.getClaim().getGuid() ) || 'new';
                        },
                        function() { // Rank selector
                                return $( '<div/>' );
@@ -162,14 +163,6 @@
        _referencesListview: null,
 
        /**
-        * The `Statement` represented by this view. This is the `Statement` 
actually stored in the data
-        * base. Updates to the `Statement` not yet stored are not reflected in 
this object.
-        * @property {wikibase.datamodel.Statement|null}
-        * @private
-        */
-       _statement: null,
-
-       /**
         * Reference to the `listview` widget managing the qualifier 
`snaklistview`s.
         * @property {jQuery.wikibase.listview}
         * @private
@@ -212,28 +205,26 @@
 
                PARENT.prototype._create.call( this );
 
-               this._statement = this.options.value;
-
-               this._createRankSelector( this._statement
-                       ? this._statement.getRank()
+               this._createRankSelector( this.options.value
+                       ? this.options.value.getRank()
                        : wb.datamodel.Statement.RANK.NORMAL
                );
-               this._createMainSnak( this._statement
-                       ? this._statement.getClaim().getMainSnak()
-                       : this.option( 'predefined' ).mainSnak || null
+               this._createMainSnak( this.options.value
+                       ? this.options.value.getClaim().getMainSnak()
+                       : this.options.predefined.mainSnak || null
                );
 
-               this._initialQualifiers = this._statement
-                       ? this._statement.getClaim().getQualifiers()
+               this._initialQualifiers = this.options.value
+                       ? this.options.value.getClaim().getQualifiers()
                        : new wb.datamodel.SnakList();
 
                // TODO: Allow adding qualifiers when adding a new claim.
-               if( this._statement && this._initialQualifiers.length ) {
+               if( this.options.value && this._initialQualifiers.length ) {
                        this._createQualifiersListview( this._initialQualifiers 
);
                }
 
                this._referencesChanger = 
this.options.entityChangersFactory.getReferencesChanger();
-               this._createReferences( this._statement );
+               this._createReferences( this.options.value );
 
                this._updateHelpMessage();
        },
@@ -464,12 +455,12 @@
         * @private
         */
        _updateHelpMessage: function() {
-               if( !this._statement && !this.options.predefined.mainSnak ) {
+               if( !this.options.value && !this.options.predefined.mainSnak ) {
                        return;
                }
 
-               var property = this._statement
-                       ? 
this._statement.getClaim().getMainSnak().getPropertyId()
+               var property = this.options.value
+                       ? 
this.options.value.getClaim().getMainSnak().getPropertyId()
                        : this.options.predefined.mainSnak.property;
 
                var deferred = $.Deferred(),
@@ -524,7 +515,7 @@
         * @return {boolean}
         */
        isInitialValue: function() {
-               if( this._statement ) {
+               if( this.options.value ) {
                        if( !this._rankSelector.isInitialValue() ) {
                                return false;
                        }
@@ -555,9 +546,15 @@
         * @private
         *
         * @param {string} guid
-        * @return {wikibase.datamodel.Statement}
+        * @return {wikibase.datamodel.Statement|null}
         */
        _instantiateStatement: function( guid ) {
+               var mainSnak = this.$mainSnak.data( 'snakview' ).snak();
+
+               if( !mainSnak ) {
+                       return null;
+               }
+
                var qualifiers = new wb.datamodel.SnakList(),
                        snaklistviews = this._qualifiers ? 
this._qualifiers.value() : [];
 
@@ -567,11 +564,7 @@
                }
 
                return new wb.datamodel.Statement(
-                       new wb.datamodel.Claim(
-                               this.$mainSnak.data( 'snakview' ).snak(),
-                               qualifiers,
-                               guid
-                       ),
+                       new wb.datamodel.Claim( mainSnak, qualifiers, guid ),
                        new wb.datamodel.ReferenceList( this._getReferences() ),
                        this._rankSelector.value()
                );
@@ -635,13 +628,14 @@
        },
 
        /**
-        * Returns the current `Statement` represented by the view. If `null` 
is returned, the view's
-        * `Statement` has not yet been stored.
+        * Returns the current `Statement` represented by the view, considering 
all pending changes not
+        * yet stored. Use `this.option( 'value' )` to retrieve the 
stored/original `Statement`.
         *
         * @return {wikibase.datamodel.Statement|null}
         */
        value: function() {
-               return this._statement;
+               var guid = this.options.value ? 
this.options.value.getClaim().getGuid() : null;
+               return this._instantiateStatement( guid );
        },
 
        /**
@@ -768,25 +762,33 @@
         *
         * @return {Object} jQuery.Promise
         * @return {Function} return.done
-        * @return {wikibase.datamodel.Statement} return.done.statement The 
saved statement-
+        * @return {wikibase.datamodel.Statement} return.done.statement The 
saved statement.
         * @return {Function} return.fail
         * @return {wikibase.api.RepoApiError} return.fail.error
+        *
+        * @throws {Error} if unable to instantiate a `Statement` from the 
current view state.
         */
        _saveStatementApiCall: function() {
                var self = this,
                        guid;
 
-               if( this.value() ) {
-                       guid = this.value().getClaim().getGuid();
+               if( this.options.value ) {
+                       guid = this.options.value.getClaim().getGuid();
                } else {
                        var guidGenerator = new 
wb.utilities.ClaimGuidGenerator();
                        guid = guidGenerator.newGuid( mw.config.get( 
'wbEntityId' ) );
                }
 
-               return this.option( 'claimsChanger' ).setStatement( 
this._instantiateStatement( guid ) )
+               var statement = this._instantiateStatement( guid );
+
+               if( !statement ) {
+                       throw new Error( 'Unable to instantiate Statement' );
+               }
+
+               return this.option( 'claimsChanger' ).setStatement( statement )
                .done( function( savedStatement ) {
                        // Update model of represented Statement:
-                       self._statement = savedStatement;
+                       self.options.value = savedStatement;
                } );
        },
 
@@ -815,7 +817,7 @@
                        var self = this;
 
                        this.$mainSnak.one( 'snakviewafterstartediting', 
function() {
-                               if( !self._qualifiers && self._statement ) {
+                               if( !self._qualifiers && self.options.value ) {
                                        self._createQualifiersListview();
                                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/179422
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If0cb699ec36f06177322ba313f2dfae853f06037
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to