如何处理前任preSS路由器方法用ajax Backbone.js的结果 [英] How to handle the express router method result in BackBone.js using ajax

查看:92
本文介绍了如何处理前任preSS路由器方法用ajax Backbone.js的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来Backbone.js的现在我需要用ajax我知道如何与jquery.But做没有使用jQuery处理前preSS路由器方法Backbone.js的结果如何与骨干做。我已经了解了骨干路由器,模型,视图和集合,但我依然不明确。

app.js

  VAR前preSS =要求('前preSS');
  VAR应用=前preSS();
  app.use(如press.bodyParser());
  app.all('*',函数(REQ,RES){      res.writeHead(200,{'的Content-Type:文/ JSON'});
      res.write(JSON.stringify(结果));
      重发();
   });
   app.listen(8080);

我处理根据要求,我需要发送page.In的头部和身体'结果'参数我送的身体和头部的前preSS的所有请求,但我不知道如何得到这一结果在Backbone.js.Below code是处理来自节点js.Is Ajax响应它是正确的。

client.js

  VAR为MyModel = Backbone.Model.extend();
  VAR MyCollection的= Backbone.Collection.extend({
    网址:/index.html',
    型号:为MyModel
});
 科尔变种=新MyCollection的(); coll.fetch({
     错误:功能(收集,响应){
        的console.log('错误',响应);
    },
    成功:函数(收集,响应){
        的console.log('成功',响应);
    }
 });


解决方案

而不是捕捉所有路由尝试像

  app.get('/型号/'功能(REQ,RES){
    res.json({{ID:1,名称:最大},{ID:2,名称:'比尔'}});
});

这是模型的范例JSON对象。

  VAR为MyModel = Backbone.Model.extend({});

这是一个空的模型

  VAR MyCollection的= Backbone.Collection.extend({
    网址:'/型号/',
    型号:型号
});

这是我们使用空模型的集合。

  VAR科尔=新MyCollection的();
coll.fetch({成功:函数(){
    的console.log(科尔);
}
});

忽略错误,只专注于成功。

这应该为你工作。一个伟大的系列教程中,我发现在开始的时候是
http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/

希望这有助于

I'm new to Backbone.js now i need to handle the express router method result in BackBone.js using ajax i know how to do it with jquery.But without using jquery how to do it with Backbone.I have learned about Backbone router,model,view and collection but still i'm not clear.

app.js

  var express=require('express');
  var app=express();
  app.use(express.bodyParser());
  app.all('*',function(req,res){

      res.writeHead(200, {'Content-Type': 'text/json'});
      res.write(JSON.stringify(result));
      res.end();
   });
   app.listen(8080);

I'm handling all request in express depending upon the request i need to send the header and body of the page.In that 'result' parameter i'm sending the body and header but i don't know how to get that result in Backbone.js.Below code is to handle the ajax response from node js.Is it correct.

client.js

  var MyModel = Backbone.Model.extend();
  var MyCollection = Backbone.Collection.extend({
    url: '/index.html',
    model: MyModel
});
 var coll = new MyCollection();

 coll.fetch({
     error: function (collection, response) {
        console.log('error', response);
    },
    success: function (collection, response) {
        console.log('success', response);
    }
 });

解决方案

Instead of catching all the routes try something like

app.get('/models/',function(req,res){
    res.json({{id: 1, name: 'max'},{id: 2, name: 'bill'}});
});

this is a sample json object of models.

var MyModel = Backbone.Model.extend({});

This is an empty model

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

A collection that uses our empty model.

var coll = new MyCollection(); 
coll.fetch({success: function () {
    console.log(coll);
}
});

Ignore the error, just focus on the success.

This should work for you. A great tutorial series I found when getting started was http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/

Hope this helps

这篇关于如何处理前任preSS路由器方法用ajax Backbone.js的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆