我今天遇到了一个奇怪的问题.对于其他人来说,这可能是一个简单的答案,但它让我难过.为什么下面的代码会导致内存错误?
var cur = 167772160;
var bcast = 184549375;
var addresses = [];
while (cur <= bcast){
cur += 1;
addresses.push(cur);
}
addresses.length
addresses // memory goes from a few megs to over a gig in seconds when trying to print this
Run Code Online (Sandbox Code Playgroud)
我得到了这两个错误中的一个......第一个是我在node的解释器中运行这个代码而后者是我在nodeunit中运行它时:
致命错误:CALL_AND_RETRY_2分配失败 - 处理内存不足
致命错误:JS分配失败 - 处理内存不足
我真的在使用require.js和jquery mobile.我有一个基于松散的文件结构和基于的加载模式
https://github.com/appboil/appboil-requirejs-backbonejs-jquerymobile-phonegap
但它已经过时了,我不得不对require 2.0版本进行调整.是否有社区接受使用jquery mobile,backbonejs和requirejs的方式?我想使用骨干网路由而不是jquery手机.此外,该模板有phonegap,我不关心.
我已经看到了从集合中获取下一个或上一个模型的几种不同方法,但是想知道是否有人可以就我决定实现它的方式提供一些建议.我的收藏是有序的,但我正在排序的ID不能保证顺序.它只保证是独一无二的.假设较小的id是集合的"较旧"条目,较大的id是"较新的".
MyCollection = Backbone.Collection.extend({
model: MyModel,
initialize:function (){
this.getElement = this._getElement(0);
},
comparator: function(model) {
return model.get("id");
},
_getElement: function (index){
var self = this;
return function (what){
if (what === "next"){
if (index+1 >= self.length) return null;
return self.at(++index);
}
if (what === "prev"){
if (index-1 < 0 ) return null;
return self.at(--index);
}
// what doesn't equal anything useful
return null;
};
}
});
Run Code Online (Sandbox Code Playgroud)
当使用getElement时,我会执行诸如getElement("next")和getElement("prev")之类的事情来询问我的集合中的下一个或上一个模型.从getElement返回的是实际模型,而不是索引.我知道collection.indexOf,但我想要一种方法来循环一个集合,而无需先从模型开始.这种实施是否比它需要的更难?
所以,我正在尝试学习如何使用Backbone,并且我一直在使用defaults对象和initialize方法之间来回切换.如果我使用该方法,则使用"this.set()"来设置属性等.否则,这些属性在默认对象中设置.
我在google上环顾四周,似乎无法找到推荐的方式或何时使用默认值或何时使用初始化的"常见"模式.我可以使我的代码兼顾两种方式,并且都产生具有所需属性的对象,但它会让我感到烦恼,因为我不确定我是否正确使用它.
快速问题...我有一个requirejs设置的定义如此...它大约8-10%的时间工作.似乎资源有时没有及时加载.我可以以确保其下方代码正确运行的方式包装上面的var require列表吗?当它不起作用时我得到的错误是这样的:
未捕获错误:尚未为上下文加载模块名称"views/association/Associations":_
define(function( require ){
// requirejs - too many includes to pass in the array
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
namespace = require('namespace'),
// models
CustomerModel = require('models/customer/customer'),
// collections
// views
BaseView = require('views/baseView'),
Auth = require('views/auth/Auth'),
SideNav = require('views/sidenav/SideNav'),
CustomersView = require('views/customer/Customers'),
AssociationsView = require('views/association/Associations'),
//CustomerListCpeView = require('views/customer/CustomerListCpe'),
//CustomerAddCpeView = require('views/customer/CustomerAddCpe'),
// templates
CustomerDetailTemplate = require('text!templates/customer/customerDetail.html');
Run Code Online (Sandbox Code Playgroud) // node v0.5.6 //
我假设nodejs可以在nodejs堆之外分配的最大缓冲区大小受可用系统内存量的限制.虽然我有几次免费内存,但我似乎无法在没有崩溃节点的情况下接近该限制.
致命错误:JS分配失败 - 处理内存不足
function bigArray(){
// each ip could be 10 digits long, therefore,
// 10 * (bcast-cur) = size of Buffer.
// does that also mean size in bytes?
var cur = 167772160;
var bcast = 184549375;
var addresses = new Buffer((bcast-cur)*10);
var offset = 0;
while (cur <= bcast){
cur += 1;
addresses.writeUInt32LE(cur,offset);
offset+=10;
}
return addresses;
};
var ba = bigArray();
Run Code Online (Sandbox Code Playgroud)
它在此块的节点库中的Buffer.js的第235行崩溃:
if (this.length > Buffer.poolSize) {
// Big buffer, just alloc …Run Code Online (Sandbox Code Playgroud) 希望这是一个简单的问题.我正在努力学习骨干,我坚持一个非常简单的事情.当我使用create方法更新集合时,视图上的渲染永远不会被调用.我认为这应该在没有显式调用render的情况下发生 我没有加载任何动态的东西,在这个脚本触发之前它都在dom中.click事件工作正常,我可以将新模型添加到集合中,但视图中的渲染永远不会触发.
$(function(){
window.QuizMe = {};
// create a model for our quizzes
QuizMe.Quiz = Backbone.Model.extend({
// override post for now
"sync": function (){return true},
});
QuizMe._QuizCollection = Backbone.Collection.extend({
model: QuizMe.Quiz,
});
QuizMe.QuizCollection = new QuizMe._QuizCollection
QuizMe.QuizView = Backbone.View.extend({
el:$('#QuizMeApp'),
template: _.template($('#quizList').html()),
events: {
"click #addQuiz" : "addQuizDialog",
},
initialize: function() {
// is this right?
_.bindAll(this,"render","addQuizDialog")
this.model.bind('add', this.render, this);
},
addQuizDialog: function(event){
console.log('addQuizDialog called')
QuizMe.QuizCollection.create({display:"this is a display2",description:"this is a succinct description"});
},
render: function() {
console.log("render called")
}, …Run Code Online (Sandbox Code Playgroud)