0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BackboneJSで、URL/LocalStorageを使いたくない人に

0
Posted at

Backboneで、簡単な画像編集画面を作ってみようと思って始めた

Screen Shot 2017-11-06 00 54 59 AM.png

まだ、未完成だがこれからのつなぎ込みを考えてるうちにlocalStorageには保存したくないなと思っていろいろ探して見たが、ダメだった


var a = new Backbone.Collection.extend({
    url: '',
    localStorage: new Store(),
})

app.as = new a();

のふたパターンがあるが、どっちもサーバーか、ローカルとも連動が必然だった

どうしよう、RailsでSPAのアプリ構築を想定してるが、そんなリアルタイムでsyncして欲しくもない

グローバル変数と連結してやるのでは

localStorage: g_value

で、collectionを定義したら、エラーは出なかった。
いけるんじゃないと思って、アプリを起動したらエラー出まくり

主に

  • linstenTo model, 'add' の原因
  • find
  • create
  • save

から問題が発生
なるほど、collectionは何もしてくれなくなったな...

collection -> model -> create instance -> return collection

この流れが崩れてる、しょがないな...自分で実装してみるか


Backbone.Model.extend({
    create: function(options){
        return new this.model({
            Object.assign(this.defaults, options)
        })
    }
    this.trigger("add")
    
    save: function(options){
        this.attributes = Object.assign(this.attributes, options)
        this.trigger("update")
    }
})

まじか、いろいろ細かいな...
全部カバーするのがめんど

でも、asから参照できない状態になった

つまり、collectionはモデル配列を更新してない
であれば、collectionを使う意味が...

最終的に

Storageを使いたくなく、グローバル、あるいはJSON_APIからのフィードバックで、十分だと思っている人は、collectionを捨てましょう

window.store = function() {
    this.value = []
    this.push: function(){}
    this.pop: function(){}
    this.uuid: function(){}
    this.find: function(){}
}

こんな感じで、作れば十分使えるようなstorageが生成される。

JointJSもCollectionをextendして使うケースがないな
Collectionはもう少し、リアルタイム性があるものを作った方がいいな

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?