Based on a working web application with (Backbone.js 1.0.0), I need to overwrite history start method, to force polling to detect hash changes, and discard other options. On a new file I do this:
var myHistory = Backbone.History.extend({
start: function (options) {
if (History.started) throw new Error("Backbone.history has already been started");
History.started = true;
// code here
}
});
Backbone.history = new myHistory;
As result, I get an Error: ReferenceError: Undefined variable: History.
any hints? Thanks!
I know this question is fairly old, and you've probably solved it by now, but I recently had to do a similar thing. For me I replaced where it said 'History.started' with 'Backbone.History.started', as History.started is a variable in the original Backbone library.
Related
I've looked everywhere and it doesn't seem like there are any questions let alone answers out there regarding this.
I've tried multiple ways and using prototypal inheritance seemed like it would be the best way
function MyWebSocket(url) {
// do other stuff
WebSocket.call(this, url);
}
MyWebSocket.prototype = Object.create(WebSocket.prototype);
MyWebSocket.prototype.constructor = MyWebSocket;
MyWebSocket.prototype.test = function () { alert('hi'); };
I can assign the test function alone just fine but in trying to override the consutructor I get the error "TypeError: Failed to construct 'WebSocket': Please use the 'new' operator, this DOM object constructor cannot be called as a function" from chrome.
I understand that this is not your typical javascript function/object but I would really like a way to override the WebSocket constructor to set in motion other maintenance functions such as a ping interval and yes I can do this individually in the onopen functions but this is something I want to just be global to all WebSockets opened on the site.
Thanks everyone!
Something like the following could work for you:
function MyWebSocket(url) {
this.ws = new WebSocket(url);
}
MyWebSocket.prototype.test = (function() {
// this = MyWebSocket.ws, i.e. a plain WebSocket obj
}).bind(MyWebSocket.ws);
var myWS = new MyWebSocket(url);
myWS.test(); // does something to the underlying websocket
Or alternately, to do something to the underlying websocket, you could not use bind and instead just refer to this.ws when defining the functions on MyWebSocket.prototype.
I am wondering how to add my own function to an object, built by a framework. So, the framework is vis.js and I want to add the function to the DataSet object.
A dataset is generated with:
var myDataSet = new vis.DataSet();
So I figured
vis.DataSet.prototype.addFormattedNode = function () {...}
could do the trick. Sadly, Chrome Dev Tool reports:
Uncaught ReferenceError: DataSet is not defined
which is probably based on the fact, that the vis framework is not yet loaded, right? I tried adding the prototype in the dev tools (after everything is loaded) and it works. One could do that, but I am looking for the "right way" without dirty hacks :-)
Any ideas or best practices for this?
Thanks!
Your solution
vis.DataSet.prototype.addFormattedNode = function () {...}
is should indeed just work. Of course that will only work if vis is loaded first, so you have to load stuff in the right order.
A more neat solution would be to create an extension of the prototype like this:
function MyDataSet (data, options) {
vis.DataSet.call(this, data, options);
}
MyDataSet.prototype = new vis.DataSet();
MyDataSet.prototype.addFormattedNode = function () {
// ...
};
Then you can use MyDataSet instead of vis.DataSet.
I just want to confirm that I'm not missing something with regards to managing context and overriding methods. I'm using the http-proxy module in a node.js app and I need to override the function HttpProxy.prototype.proxyRequest. I'd like to do it without modifying the original module code directly but haven't been able to find a way to do it.
If I do this:
var httpProxy = require('http-proxy'),
httpProxyOverride = require('./http-proxy-override.js');
httpProxy.HttpProxy.prototype.proxyRequest = httpProxyOverride.proxyRequestOverride;
Then I lose the original context and errors are thrown. If I use apply(), I can provide a new context, but it doesn't appear I can persist the original context.
Based off of this SO thread:
Is it possible to call function.apply without changing the context?
It doesn't appear that there is a way to achieve what I'm trying to do and I'm hoping that someone can confirm this or correct me if I'm wrong.
What about saving the old function and then overwriting it like:
var old = httpProxy.HttpProxy.prototype.proxyRequest;
httpProxy.HttpProxy.prototype.proxyRequest = function () {
old.apply(this, arguments);
//do more stuff
}
taken from Javascript: Extend a Function
I recently updated to the latest version of Node.js (1.10~) from 0.8~, and I've been getting a message when running that says:
util.pump() is deprecated. Use readableStream.pipe() instead.
I've tried to switch my functions to say readableStream.pipe(), but I don't think it's working the same.
So I have three questions:
Why is util.pump deprecated?
How do I switch to readableStream.pipe()?
OR 3. How do I turn off this warning?
Here is the code where I'm using it (with mustache)
var stream = mu.compileAndRender(template_file, json_object_from_db);
util.pump(stream, res);
When I replace util.pump with readableStream.pipe, I get this error:
ReferenceError: readableStream is not defined
Can anyone help point me in the right direction?
Okay, so this question was a pretty easy answer after some more experimentation (though documentation was null).
Basically, readableStream is just a variable you're supposed to replace with your stream. So in my case, the answer is:
stream.pipe(res);
You just replace util, basically, with the stream. Easy peezy.
I Think the following link will help for your work ! https://groups.google.com/forum/#!msg/nodejs/YWQ1sRoXOdI/3vDqoTazbQQJ
var readS = fs.createReadStream("fileA.txt");
var writeS = fs.createWriteStream("fileB.txt");
util.pump(readS, writeS, function(error)
// Operation done
});
=====>
var readS = fs.createReadStream("fileA.txt");
var writeS = fs.createWriteStream("fileB.txt");
readS.pipe(writeS);
readS.on("end", function() {
// Operation done
});
You can look this link http://nodejs.cn/api/stream.html
This is emitted whenever the stream.pipe() method is called on a readable stream, adding this writable to its set of destinations.
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('pipe', (src) => {
console.error('something is piping into the writer');
assert.equal(src, reader);
});
reader.pipe(writer);
Please bear with me as I'm new to JS and am having trouble implementing some things with Meteor. I implemented a class in JavaScript using
function Class() {
this.property = 0
this.method = function () {
return "method called"
}
}
I made a new Meteor Collection bu using new Meteor.collection and successfully retrieved the data on the client and can display Class.property in the html template. However, I am unable to access Class.method and was wondering if there's any way to make this happen and if using Meteor.methods to define functions that take the Class instance as input is the best way to go.
For anyone still looking at this, the reason the code doesn't work is because mongodb stores documents as bson. bson, just like json, does not support functions (http://bsonspec.org) so when the above class is saved by meteor into mongo, the method is not saved as part of the document.
There is no easy elegant solution I'm aware of. I have the same issue. In order to utilise the class method you would need to instantiate the class each time you needed it, which you could implement as part of a database model.
This is not really an answer but in meteor's package manager you can add libraries like backbone.js which gives you models, collection and views and a nice router which I find very handy when making meteor apps. Backbone works well with jQuery.
My other suggestion is using a library like Mootools which unlike jQuery doesn't try to change the way you write javascript but enhancing the experience of making object oriented javascript. (see: jqueryvsmootools). With mootools you can can make a class the following way...
var MyClass = new Class({
'Implements': [Options],
//default options
'options': {
'foo': null
},
'initialize': function(options) {
this.foo = options.foo;
},
'bar' : function() {
return this.foo;
}
});
var blub = new MyClass({'foo': 'Hello World'});
blub.bar(); // "Hello World"
I was looking to do the same thing.
I found a function called "transform" that is called when getting something from a meteor collection. You can use it to add a function to a meteor object just as you require.
Here is an example of adding an "endDate" function and "remaining" functions to a meteor object
Products = new Meteor.Collection("Products", {
transform: function (doc) {
doc.endDate = function () {
// SugarJS gives us minutesAfter() which gives us a nice syntax for
// creating new Date objects
// http://sugarjs.com/api/Number/unitAfter
return ((25).minutesAfter(this.startDate));
};
doc.remaining = function () {
return this.endDate().getTime() - Date.now();
};
return doc;
}
});
Read more here:
http://www.okgrow.com/posts/2014/05/19/meteor-transform/
This approach worked beautifully for me:
http://www.okgrow.com/posts/2014/05/19/meteor-transform/
I don't know anything about Meteor, but I see a problem with your code. You're missing a semi-colon after:
this.property = 0
Without that semi-colon, the javascript interpreter will not execute the this.method assignment.