我打算使用以下模式来使用基于requireJS的模块作为单例.请注意,classA返回类型为'classA'的实例,而classB,classC和main的其余类返回模块中类的类型.所有这些都是基于MooTools类的类.
我们的想法是将classA用作全局可用的单例,这些方法只是填充程序.如果这是一个可以接受的模式使用的任何想法?
这会在稍后阶段回来咬我吗?我还没试过在项目上运行r.js,所以我有点担心,并寻找一些建议.
// classA.js
define([], function() {
var classA = new Class({
initialize: function (regionId) {
// perform some Initialization.
this.data = null;
},
doSomething: function(param) {
// some thing.
this.data = param;
}
};
return new classA();
});
// classB.js
define(["classA"], function(classA) {
var classB = new Class({
initialize: function (regionId) {
// perform some Initialization.
},
doSomethingElse: function() {
// some thing.
classA.doSomething("Go back to Work Now!");
}
};
return classB; …Run Code Online (Sandbox Code Playgroud) 这是一个来自JS新手的两部分问题.
所以,我试图通过跟随Thomas Davis的教程使用requireJS创建一个骨干应用程序.
如何通过对提供XML数据的服务器的ajax调用来创建Backbone集合?collections.fetch()似乎期待一个JSON后端.
在尝试一些事情时,我最终得到了以下代码,其中在从Ajax成功回调中填充集合"bookStore"时页面不会刷新.
到目前为止,我已经走了多远.
var bookListView = Backbone.View.extend({
el: $("#books"),
initialize: function () {
thisView = this;
$.ajax({
type: "GET",
url: "books.xml",
dataType: "xml",
success: function (data) {
console.log(data);
$(data).find('book').each(function (index) {
var bookTitle = $(this).find('name').text();
bookStore.add({ title: bookTitle });
console.log(seid);
});
thisView.collection = bookStore;
thisView.collection.bind('add', thisView.tryBind);
}
}).done(function (msg) {
alert("Data retrieved: " + msg);
});
this.collection = bookStore;
this.collection.bind("add", this.exampleBind);
this.collection.bind("refresh", function () { thisView.render(); });
/*
// This one works!
bookStore.add({ name: "book1" …Run Code Online (Sandbox Code Playgroud)我正面临这个奇怪的问题.我正在调试的WebApp,正在调用javascript console.log/console.log/error/debug/etc.,但Firebug控制台根本不打印它们.此应用程序使用Dojo/Dijit工具包.不确定它是否有任何特殊之处
它似乎不是浏览器的问题,我尝试了另一个带有console.debug调用的简单网页,并且消息按预期显示在控制台上.
请告知我应该寻找什么.我也试过Chrome/IE.
提前致谢/
我在GitHub中查看Backbone-requireJS样板,我看到了两种不同类型的实现.
https://github.com/david0178418/BackboneJS-AMD-Boilerplate/blob/master/src/js/views/viewStub.js具有以下作为viewStub:
function() {
"use strict";
define([
'jqueryLoader',
'underscore',
'backbone',
],
function($, _, Backbone) {
return Backbone.View.extend({
template : _.template(/*loaded template*/),
initialize : function() {
this.render();
},
render : function() {
$(this.el).append(this.template(/*model/collection*/));
return this;
}
});
}
);
})();
Run Code Online (Sandbox Code Playgroud)
而来自其他样板的视图存根 https://github.com/jcreamer898/RequireJS-Backbone-Starter/blob/master/js/views/view.js具有以下内容:
define([
'jquery',
'backbone',
'underscore',
'models/model',
'text!templates/main.html'],
function($, Backbone, _, model, template){
var View = Backbone.View.extend({
el: '#main',
initialize: function(){
this.model = new model({
message: 'Hello World'
});
this.template = _.template( template, { model: this.model.toJSON() } );
}, …Run Code Online (Sandbox Code Playgroud) 我试图学习使用Jasmine测试基于Backbone的应用程序的方法.为此,我从这里获取了一些示例代码:http://msdn.microsoft.com/en-us/scriptjunkie/hh377172.
Photo = new Backbone.Model.extend({
defaults:{
title: 'Another photo!',
tags: ['untagged'],
location: 'home',
src: 'placeholder.jpg'
},
initialize: function(){
console.log('this model has been initialized');
this.bind("change:title", function(){
var title = this.get("title");
console.log("My title has been changed to.." + title);
});
},
setTitle: function(newTitle){
this.set({ title: newTitle });
}
});
Run Code Online (Sandbox Code Playgroud)
然后编写测试规范如下:
describe("Photo Model", function() {
it("verifies title", function() {
var myPhoto = new Photo();
myPhoto.set({ title: "On the beach" });
expect(myPhoto.get("title"))
.toEqual("On the beach");
});
});
Run Code Online (Sandbox Code Playgroud)
在运行它时,测试因TypeError而失败
TypeError: Object …Run Code Online (Sandbox Code Playgroud)