{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/backbone/issues/4004","repository_url":"https://api.github.com/repos/jashkenas/backbone","labels_url":"https://api.github.com/repos/jashkenas/backbone/issues/4004/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/backbone/issues/4004/comments","events_url":"https://api.github.com/repos/jashkenas/backbone/issues/4004/events","html_url":"https://github.com/jashkenas/backbone/pull/4004","id":145361416,"number":4004,"title":"When model.id is set in model.initialize, Collection.get('mymodelid') will return undefined","user":{"login":"jammon","id":87003,"avatar_url":"https://avatars.githubusercontent.com/u/87003?v=3","gravatar_id":"","url":"https://api.github.com/users/jammon","html_url":"https://github.com/jammon","followers_url":"https://api.github.com/users/jammon/followers","following_url":"https://api.github.com/users/jammon/following{/other_user}","gists_url":"https://api.github.com/users/jammon/gists{/gist_id}","starred_url":"https://api.github.com/users/jammon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jammon/subscriptions","organizations_url":"https://api.github.com/users/jammon/orgs","repos_url":"https://api.github.com/users/jammon/repos","events_url":"https://api.github.com/users/jammon/events{/privacy}","received_events_url":"https://api.github.com/users/jammon/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-02T10:46:37Z","updated_at":"2016-04-04T19:23:29Z","closed_at":"2016-04-04T17:33:09Z","pull_request":{"url":"https://api.github.com/repos/jashkenas/backbone/pulls/4004","html_url":"https://github.com/jashkenas/backbone/pull/4004","diff_url":"https://github.com/jashkenas/backbone/pull/4004.diff","patch_url":"https://github.com/jashkenas/backbone/pull/4004.patch"},"body":"I have a model for a day that calculates its id like this:\r\n```javascript\r\n    var Day = Backbone.Model.extend({\r\n      initialize: function() {\r\n        // set id to something like '20160402'\r\n        var date = this.get('date');\r\n        this.id = '' + (date.getFullYear() * 10000 +\r\n                        (date.getMonth()+1) * 100 +\r\n                        date.getDate());\r\n      },\r\n    });\r\n```\r\nWhen I add this to a Collection I cannot retrieve it via Collection.get:\r\n```javascript\r\n    var day = new Day({'date': new Date(2016, 3, 2)});\r\n    var days = new Backbone.Collection([day]);\r\n    assert.equal(days.at(0).id, '20160402');  // this succeeds\r\n    assert.notEqual(days.get('20160402'), void 0);  // this fails\r\n```\r\nThis is somewhat unexpected. I can change that behaviour by changing Collection._addReference:\r\n```javascript\r\n    // Internal method to create a model's ties to a collection.\r\n    _addReference: function(model, options) {\r\n      this._byId[model.cid] = model;\r\n      // var id = this.modelId(model.attributes); // changed to:\r\n      var id = model.id || this.modelId(model.attributes);\r\n      if (id != null) this._byId[id] = model;\r\n      model.on('all', this._onModelEvent, this);\r\n    },\r\n```\r\nBut this breaks a different test ('Collection with polymorphic models receives default id from modelId').\r\n\r\nI'm not sure, what the intended behaviour is. If the behaviour is meant like it is, it should be stated in the docs."},"comment":{"url":"https://api.github.com/repos/jashkenas/backbone/issues/comments/205456181","html_url":"https://github.com/jashkenas/backbone/pull/4004#issuecomment-205456181","issue_url":"https://api.github.com/repos/jashkenas/backbone/issues/4004","id":205456181,"user":{"login":"jammon","id":87003,"avatar_url":"https://avatars.githubusercontent.com/u/87003?v=3","gravatar_id":"","url":"https://api.github.com/users/jammon","html_url":"https://github.com/jammon","followers_url":"https://api.github.com/users/jammon/followers","following_url":"https://api.github.com/users/jammon/following{/other_user}","gists_url":"https://api.github.com/users/jammon/gists{/gist_id}","starred_url":"https://api.github.com/users/jammon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jammon/subscriptions","organizations_url":"https://api.github.com/users/jammon/orgs","repos_url":"https://api.github.com/users/jammon/repos","events_url":"https://api.github.com/users/jammon/events{/privacy}","received_events_url":"https://api.github.com/users/jammon/received_events","type":"User","site_admin":false},"created_at":"2016-04-04T19:23:29Z","updated_at":"2016-04-04T19:23:29Z","body":"Thank you for the explanation!"}}