enyo.Model.isNew and enyo 2.5

It seems enyo.Model.isNew has been removed.

Now the models use enyo.States, but i am not quite sure how and when the states are applied.

The documentation is still using model.isNew to decide whether to POST or PUT.

I just need a quick way to tell if the item needs to be updated or inserted.

Comments

  • Hi Chris,

    Quick answer: You need to check model.status for enyo.States.NEW.

    For a more detailed answer, I suggest taking a look at the source in /enyo/source/data/Model.js. It's not terribly complicated. STATES.NEW is only cleared after a fetch.
  • Ok, just to make sure this is correct:

    I have some cases where I need to create a new Model, and just set a few values on it, instead of having to fetch the full records dataset using fetch first. Usually i just the primary key, and some data that has changed.
    Now, when i commit the newly, locally created model, i want it to do a PUT instead of POST. I could set it up to just send a flag with opts, but the way i was doing it before was setting isNew to false before i commit.

    So to remove the STATES.NEW, i would use this code:
    var m = new enyo.Model({test: "test"});
    m.status = m.status & ~enyo.States.NEW;
    Playing with it in the debugger seems to set m.status to 0. Is this the correct way?
  • So it seems to be working ok with the above code.

    Is there a reason that the NEW state isn't cleared when the model is commited as well?
    It would seem to me that once a model is saved to the server, it shouldn't be counted as "new", since it has already been saved.

    In my custom model, i made some functions for my purposes:
    setNew: function(isnew) {
    		if (isnew) {
    			this.status = this.status & enyo.States.NEW;
    		}
    		else {
    			this.status = this.status & ~enyo.States.NEW;
    		}
    	},
    	isNew: function() {
    		return this.status & enyo.States.NEW;
    	}
    And i made my custom model clear the new flag after a commit.
    committed: enyo.inherit(function(sup) {
    		return function() {
    			sup.apply(this, arguments);
    			this.setNew(false);
    			console.log(this.kindName + ": Commit Success");
    		};
    	}),
  • First: You should probably use this.set("status", this.status & ... ) in the case that you would want to observe changes to the status.

    Second: I was wondering whether NEW should be cleared after a commit. I will make a note to discuss it with Cole.
Sign In or Register to comment.