小编Jag*_*ags的帖子

如何使用backbone.js创建和处理Ajax调用

我是骨干的新手.我尝试以下面的方式进行ajax调用.而且我不确定如何处理我的get请求中的数据?这是代码:

//与服务器交互

var UserModel = Backbone.Model.extend({
    url:'http://api.geonames.org/astergdemJSON?formatted=true&lat=50.01&lng=10.2&username=demo&style=full'
});

var MyView = Backbone.View.extend({
    initialize: function() {
        this.model = new UserModel();
        this.model.fetch();
    },
    render: function() {
        alert('do awesome stuff here');
    }
});
Run Code Online (Sandbox Code Playgroud)

ajax backbone.js backbone-views

3
推荐指数
1
解决办法
5423
查看次数

在backbone.js中重新渲染后未触发的事件

我在重新渲染后尝试单击提交时遇到问题.

这是我的看法:

ShareHolderInfoView = Backbone.View.extend( {
template : 'shareholderinfo',
initialize: function() {
    this.model = new ShareHolderInfoModel();
},
render : function() {
    $.get("shareholderinfo.html", function(template) {
        var html = $(template);
        that.$el.html(html);
    });

    //context.loadViews.call(this);
    return this;
},
events:{
    "change input":"inputChanged",
    "change select":"selectionChanged",
    "click input[type=submit]":"showModel"
},
inputChanged:function(event){
    var field = $(event.currentTarget);
    var data ={};
    data[field.attr('id')] = field.val();
    this.model.set(data);
},
showModel:function(){
    console.log(this.model.attributes);
    alert(JSON.stringify(this.model.toJSON()));
}
});
Run Code Online (Sandbox Code Playgroud)

这是我的路由器

var shareholderInfo,  accountOwnerInfo;

App.Router = Backbone.Router.extend({

    routes:{
        'share':'share',
        'joint':'joint'
    },
    share:function(){

        $("#subSection").empty();
        if(!shareholderInfo){
            shareholderInfo = new ShareHolderInfoView();
            $("#subSection").append(shareholderInfo.render().el);
        } …
Run Code Online (Sandbox Code Playgroud)

backbone.js backbone-events backbone-views backbone-routing

1
推荐指数
1
解决办法
1263
查看次数