jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.09k stars 5.39k forks source link

on model set two change events are triggered one 'change:attributename' and the other 'change' #4197

Closed einsteinreloaded closed 6 years ago

einsteinreloaded commented 6 years ago

I am trying to write unit test cases and i was spying on trigger method of a model with sinon.stub. When i checked the triggerspy called numbers it showed me 2 and the call args were 'change:percentComplete' and 'change' events. I am using backbonejs with coffeescript.

unit test case 
        it 'should set percentComplete attribute to 100', ->
            VideoComponentMixin.model.set('isLocked',true)
            @setSpy = sinon.spy(VideoComponentMixin.model,"set")
            @triggerStub = sinon.stub(VideoComponentMixin.model,'trigger')
            @shouldMakeProgressCallStub = sinon.stub(VideoComponentMixin, 'shouldMakeProgressCall').returns(true)
            VideoComponentMixin.updateVideoComponentProgress()
            expect(VideoComponentMixin.model.get('percentComplete')).to.equal(100)
            sinon.assert.calledOnce(@triggerStub)

updateVideoComponentProgress the function i am testing
    updateVideoComponentProgress: (quiz)->
        if @model.get('isLocked')
            if @shouldMakeProgressCall(quiz)
                if @model.get('percentComplete') isnt 100
                    @model.set 'percentComplete', 100
                else
                    @model.trigger 'change:percentComplete'
paulfalgout commented 6 years ago

I'm not certain of the question. You seem to be describing how backbone works:

From the docs on events http://backbonejs.org/#Events-catalog

"change" (model, options) — when a model's attributes have changed. "change:[attribute]" (model, value, options) — when a specific attribute has been updated.

Both of these events will always be triggered when an attribute changes