简体   繁体   English

JSON的主干FETCH,编辑模型数据,然后保存回JSON

[英]Backbone FETCH from JSON, edit model data then SAVE back to JSON

I've been working through Code School's Anatomy of Backbone.js course, but am confused when trying to save model changes back to the server. 我一直在学习Code School的Backbone.js解剖课程,但是在尝试将模型更改保存回服务器时感到困惑。 Perhaps you can help. 也许你可以帮忙。

This is what I understand needs to happen: 我理解这是需要发生的:

  1. Populate collection from a JSON data source using fetch(); 使用fetch()从JSON数据源填充集合;
  2. Append the collection to the DOM 将集合追加到DOM
  3. Edit a model (uncheck checkbox, which sets 'favourite' to false) 编辑模型(取消选中复选框,将“收藏夹”设置为false)
  4. Save the model. 保存模型。

My assumption is that if I were to unselect a record as a 'favourite' then hit refresh, the change would be persistant and also evident in the JSON file. 我的假设是,如果我取消选择一条记录作为“收藏夹”,然后单击“刷新”,则更改将是持久的,并且在JSON文件中也很明显。 However, this isn't the case and the original collection is loaded and JSON is unchanged. 但是,情况并非如此,原始集合已加载且JSON不变。

I think my confusion is in using the fetch method and declaring the URL within the model and collection. 我认为我的困惑在于使用fetch方法并在模型和集合中声明URL。

How can I get this model change to be persistant? 我如何才能使模型更改持久化?

Model: 模型:

var Contact = Backbone.Model.extend({
    url: '/contacts',
    defaults:{
        favourite: false
    },
    toggleFavourite: function(){
        if(this.get('favourite') === false) 
        {
            this.set({ 'favourite': true });
        } else {
            this.set({ 'favourite': false })
        }
        this.save();
    }
});

Collection 采集

var Contacts = Backbone.Collection.extend({ 
    model: Contact,
    url: '/contacts'
});

Views 观看次数

var ContactView = Backbone.View.extend({
    className: 'record',
    template: _.template('<span><%= name %></span>' + 
                         '<span class="phone-number"><%= phone %></span>' +
                         '<input type="checkbox" <% if(favourite === true) print("checked") %>/>'),

    events: {
        'change input': 'toggleFavourite',
        'click .phone-number': 'dial'
    },

    initialize: function(){
        this.model.on('change', this.render, this);
    },

    toggleFavourite: function(e){
        this.model.toggleFavourite();
    },

    dial: function(e){
        alert('Dialing now...');
    },

    render: function(){
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});

var ContactsView = Backbone.View.extend({

    initialize: function(){
        this.collection.on('add', this.addOne, this);
        this.collection.on('reset', this.addAll, this);
    },

    addOne: function(contact){
        var contactView = new ContactView({ model: contact });
        this.$el.append(contactView.render().el);
    },

    addAll: function(){
        this.collection.forEach(this.addOne, this);
    },

    render: function(){
        this.addAll();
    }

});

App.js App.js

var contacts = new Contacts(); //creates list
contactsView = new ContactsView({ collection: contacts}); //creates list view
contacts.fetch({url: 'contacts/data.json'}); //populates list
$('#mainPanel').append(contactsView.el); //appends list to DOM

Backbone works on client, and can't change file on server itself. 骨干网可以在客户端上运行,不能在服务器上更改文件。

You need to store dynamic data somewhere on server (maybe mongodb if you use json it will be easier). 您需要将动态数据存储在服务器上的某个位置(如果使用json,则可能是mongodb,这会更容易)。

contacts/data.json named static file. Contacts / data.json命名为静态文件。 because it is not changing while you did't owerwrite it on the server. 因为当您未在服务器上对其进行扩容时它没有改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM