te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Backbone.js: Collection containing multiple Models with same ID - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js: Collection containing multiple Models with same ID - Stack Overflow

programmeradmin1浏览0评论

I've a merged collection in Backbone which contains photos and albums.

To distinguish between them, i've added a field type which is either photo or album. When I populate the collection, I create different models within the Collection#model method

  model: (attrs, options) ->
    switch attrs.type
      when 'album' then new App.Models.Album(attrs, options)
      when 'photo' then new App.Models.Photo(attrs, options)

Now I've discoverd a strange bug where adding a photo and an album with the same ID (let's say 2) results in a merge.

I've tracked this down to these LOC in the source code. It seems that it's undoable without creating a fork of Backbone itself. I've tried it but it also fails 35 tests.

I thought of 4 different ways of doing this, I don't know which of them is the better one:

  1. I could add a prefix to the id. Let's say photo_2. This causes a change in the backend as well as some changes in the frontend to don't hit the server at /photos/photo_2
  2. I could fork Backbone and change these LOC.
  3. I could create two separate collections but have to deal with a merge and a sort in the view (which effects clients performance and requires a rewriting of the backend)
  4. I could start with a photo ID of, let's say 1000000. This would extremely decrease the probability that a given user which has uploaded a photo with a given ID has also created an album with the same ID.

I've a merged collection in Backbone which contains photos and albums.

To distinguish between them, i've added a field type which is either photo or album. When I populate the collection, I create different models within the Collection#model method

  model: (attrs, options) ->
    switch attrs.type
      when 'album' then new App.Models.Album(attrs, options)
      when 'photo' then new App.Models.Photo(attrs, options)

Now I've discoverd a strange bug where adding a photo and an album with the same ID (let's say 2) results in a merge.

I've tracked this down to these LOC in the source code. It seems that it's undoable without creating a fork of Backbone itself. I've tried it but it also fails 35 tests.

I thought of 4 different ways of doing this, I don't know which of them is the better one:

  1. I could add a prefix to the id. Let's say photo_2. This causes a change in the backend as well as some changes in the frontend to don't hit the server at /photos/photo_2
  2. I could fork Backbone and change these LOC.
  3. I could create two separate collections but have to deal with a merge and a sort in the view (which effects clients performance and requires a rewriting of the backend)
  4. I could start with a photo ID of, let's say 1000000. This would extremely decrease the probability that a given user which has uploaded a photo with a given ID has also created an album with the same ID.
Share Improve this question edited Feb 4, 2014 at 14:21 George Reith 13.5k18 gold badges81 silver badges151 bronze badges asked Jun 3, 2013 at 17:29 Philipp SpiessPhilipp Spiess 3,2715 gold badges28 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Since version 1.2 you can use Collection.modelId to specify how your collection will uniquely identify models. In your case, you can do the following to ensure that your types have different IDs.

  var MyCollection = Backbone.Collection.extend({
    modelId: function (attrs) {
      return attrs.type + "-" + attrs.id;
    }
    // ...
  })

I would suggest that on both Album and Photo, you add the following:

  idAttribute: 'uniqueId'
  parse: function(response) {
    response.uniqueId = type+'_'+response.id
    return response;
  }

idAttribute:'uniqueId'

if this uniqueId is not known while declaring, try

idAttribute:'UUID'

I generated one from https://www.uuidgenerator/ and put it here, this attribute defined here need not be in model, so I just put in a UUID.

发布评论

评论列表(0)

  1. 暂无评论