revealing-module-pattern

TypeScript code similar to Revealing Module Pattern structure

流过昼夜 提交于 2019-12-05 16:48:00
I want to convert some JavaScript code I've written into TypeScript. I'm rather new to TypeScript syntax and way of thinking, as a JavaScript developer. What is giving me a headache is the hard time I've had to convert some piece of code that uses the Revealing Module Pattern into TypeScript. One example is the below: //JS Code var obj; //code... (function(){ function myFunction(){ //do work } function MyOtherConstructor(){ return { publicMethod: myFunction } } obj = new MyOtherConstructor(); })(); //use obj.publicMethod in code later One workaround I've thought was this: //TypeScript code var

Expose private variables in Revealing Module Pattern

两盒软妹~` 提交于 2019-12-03 09:25:08
问题 I'm trying to implement the Revealing Module Pattern but I'm unable to expose a modified private property. var myRevealingModule = (function(){ var name = 'Diogo'; function setName () { name = name + ' Cardoso'; } return { fullName: name, set: setName }; }()); // Sample usage: myRevealingModule.set(); console.log(myRevealingModule.fullName); // "Diogo" instead of the excepted "Diogo Cardoso". 回答1: return { fullName: name, set: setName }; That uses the values of name and setName . It does not

Expose private variables in Revealing Module Pattern

让人想犯罪 __ 提交于 2019-12-02 23:38:47
I'm trying to implement the Revealing Module Pattern but I'm unable to expose a modified private property. var myRevealingModule = (function(){ var name = 'Diogo'; function setName () { name = name + ' Cardoso'; } return { fullName: name, set: setName }; }()); // Sample usage: myRevealingModule.set(); console.log(myRevealingModule.fullName); // "Diogo" instead of the excepted "Diogo Cardoso". return { fullName: name, set: setName }; That uses the values of name and setName . It does not create a reference to the variable. Effectively, name is copied. You need to create a corresponding getName

How can I create an object of fixed structure?

落爺英雄遲暮 提交于 2019-12-01 11:14:26
I have the following code inside my revealing module, but I am uncertain with how to declare/define imageListItem , which is strictly a DTO and doesn't really require any information hiding. Am I correctly defining this object? var imageListItem = function() { var _title; Object.defineProperty(this, "title", { get: function () { return _title; }, set: function (value) { _title = value; } } ); }; var imageList = (function () { var buffer = new CBuffer(); return { populate: function (listItems) { buffer.push(listItems); }, rotate: function() { buffer.rotateLeft(); } } })(); With imageListItem ,

How can I create an object of fixed structure?

喜夏-厌秋 提交于 2019-12-01 09:41:54
问题 I have the following code inside my revealing module, but I am uncertain with how to declare/define imageListItem , which is strictly a DTO and doesn't really require any information hiding. Am I correctly defining this object? var imageListItem = function() { var _title; Object.defineProperty(this, "title", { get: function () { return _title; }, set: function (value) { _title = value; } } ); }; var imageList = (function () { var buffer = new CBuffer(); return { populate: function (listItems)

Knockout.js mapping a JSON into an observable-array

旧街凉风 提交于 2019-11-30 17:17:31
I want to build a client for my REST-Service using Knockout.js. I have a lot of Repositorys i want to access through different urls - so i came up with this solution using the Revealing-Prototype-Pattern. My problem : I can not find out how to map the ItemsProperty with my "data" i receive from my service. var Repository = function (url) { this.Url = url; this.Items = ko.observableArray([]); this.PendingItems = ko.observableArray([]); }; Repository.prototype = function () { var getAllItems = function () { var self = this; $.getJSON(self.Url, function (data) { // data=[{"Id":1,"Name":"Thomas",

How can I view the outline in eclipse when using the revealing module pattern?

徘徊边缘 提交于 2019-11-30 03:53:08
I'm currently refactoring some Javascript code we have and amongst other things I've changed it to make use of the revealing module pattern. The code is looking much tidier and it works fine but I can't see the functions anymore in the outline view. I see the top level namespace var as a var but you can't expand it to see the functions within. Lets say the code used to look like this: function myFunc1() {} function myFunc2() {} In this case you see both functions in the outline view. But if you change it to this: var myNamespace = function() { function myFunc1() {} function myFunc2() {} return

Knockout.js mapping a JSON into an observable-array

安稳与你 提交于 2019-11-30 00:48:37
问题 I want to build a client for my REST-Service using Knockout.js. I have a lot of Repositorys i want to access through different urls - so i came up with this solution using the Revealing-Prototype-Pattern. My problem : I can not find out how to map the ItemsProperty with my "data" i receive from my service. var Repository = function (url) { this.Url = url; this.Items = ko.observableArray([]); this.PendingItems = ko.observableArray([]); }; Repository.prototype = function () { var getAllItems =

Module pattern- How to split the code for one module into different js files?

只谈情不闲聊 提交于 2019-11-29 20:31:26
For the module pattern, I'm doing something like: (function(namespace) { // tons of code // blabla })(window.myGlobalNamespace); How do I split the code? I can think of a few ways, like use a hierachy of namespaces, or expand the object outside by window.myGlobalNamespace.additionalFunc = function () {//blabla} . What are the other ways? What are the pros and cons? Which one is considered better practice? Both of the two answers suggest RequireJS. Can you please explain how RequireJS can solve these problems: first.js: (function(context) { var parentPrivate = 'parentPrivate'; })(window

Why does the { position affects this Javascript code?

给你一囗甜甜゛ 提交于 2019-11-29 14:36:59
I spent a fair bit of time on this Javascript issue (you can tell I am a JS noob): Take some well written Javascript code like this example of the Revealing Module Pattern: Running it works fine. Then move the "{" to the next line (as a C# developer I set up all my environments to put curly braces on new lines) and run it again. return { someMethod : myMethod, someOtherMethod : myOtherMethod }; It now gets quite a few JS errors around "13 Line breaking error 'return'." and "Uncaught SyntaxError: Unexpected token : " in Chrome Debugger. My question is, how can something syntactically affect the