我有一个骨干应用程序和一个RESTful api。我使用Coenraets创建的示例来了解主干应用的架构,但我决定设置自己的结构并仅使用数据进行测试。使用RESTful api与骨干
我想知道从RESTful api返回数据的最佳方式。我目前有我的应用程序文件夹结构设置与模型,集合,视图和服务文件夹。我有一个运行着express的节点服务器来处理后端,并且工作正常。
我想知道的是访问restful data api的最佳做法是什么?我应该在我的服务课程还是在视图课程中这样做?我如何使用我平静的api返回的数据动态地进行这项工作:http://localhost:3000/employees
看起来有很多方法可以做到这一点,现在我只想要一些可行的方法,但最终我想知道什么是最好的方式来做到这一点。最终我想要一个CRUD设置。但我不确定应该在哪里设置。类似于在这里详细描述:http://www.codeproject.com/Articles/797899/BackBone-Tutorial-Part-CRUD-Operations-on-Backbone
我的文件如下:
employeecolletion.js
var Backbone = require('backbone');
var Employee = require('../models/employeemodel.js');
module.exports = Backbone.Collection.extend({
model: Employee,
url:"http://localhost:3000/employees"
});
employeemodel.js
var Backbone = require('backbone');
var EmployeeCollection = require('../collections/employeecollection.js');
module.exports = Backbone.Model.extend({
urlRoot:"http://localhost:3000/employees"
// initialize:function() {
// this.reports = new EmployeeCollection();
// //this.reports.url = this.urlRoot + "/" + 1 + "/reports";
// }
});
employee.js(员工认为结合到我的模板)
var fs = require('fs');
var base = require('./base.js');
var EmployeeList = require('../collections/employeecollection.js');
var employeeService = require('../services/employeeService.js');
var template = fs.readFileSync('app/templates/employee.mu', { encoding: 'utf8' });
module.exports = base.extend({
el: '.view',
template:template,
collection: employeeService.collection,
initialize: function() {
this.viewModel = {
employee_list: this.collection.toJSON()
//employee_list: this.collection.fetch() --HERE I EXPERIMENTED WITH FETCHING THE DATA
};
this.render();
}
});
个
employeeservice.js(在服务文件夹中的文件,将理想回报,我只想绑定到我的模板中,他们的员工查看文件的集合)
var EmployeeCollection = require('../collections/employeecollection.js');
//if wanting to pass in data manually
var employee_list = [
{
id:1,
firstName:"James",
lastName:"King",
fullName:"James King",
managerId:0,
managerName:"",
title:"President and CEO",
department:"Corporate",
cellPhone:"617-000-0001",
officePhone:"781-000-0001",
email:"[email protected]",
city:"Boston, MA",
pic:"james_king.jpg",
twitterId:"@fakejking",
blog:"http://coenraets.org"
}
];
//HERE I WAS EXPERIMENTING WITH A DIFFERENT SYNTAX TO DO THE FILTERING BY ID
//IN MY SERVICE AND SIMPLY RETURNING THE FINAL DATA I WANT TO MY VIEW CLASS
// var employees = new EmployeeCollection({id: id});
// employees.fetch({
// success: function (data) {
// console.log(data);
// }
// });
module.exports = {
collection: new EmployeeCollection(employee_list)
};
我不太了解制作自定义API,但我用过的大多数API只是收集和组织在json对象中。 – Popatop15