小编fus*_*sio的帖子

NodeJS中的JavaScript OOP:怎么样?

我习惯于Java中的经典OOP.

使用NodeJS在JavaScript中进行OOP的最佳实践是什么?

每个Class都是一个文件module.export

如何创建类?

this.Class = function() {
    //constructor?
    var privateField = ""
    this.publicField = ""
    var privateMethod = function() {}
    this.publicMethod = function() {} 
}
Run Code Online (Sandbox Code Playgroud)

vs.(我甚至不确定它是否正确)

this.Class = {
    privateField: ""
    , privateMethod: function() {}

    , return {
        publicField: ""
        publicMethod: function() {}
    }
}
Run Code Online (Sandbox Code Playgroud)

this.Class = function() {}

this.Class.prototype.method = function(){}

...
Run Code Online (Sandbox Code Playgroud)

继承如何工作?

是否有在NodeJS中实现OOP的特定模块?

我发现了一千种不同的方法来创造类似于OOP的东西..但我不知道最常用/实用/干净的方式是什么.

奖金问题:与MongooseJS一起使用的建议"OOP风格"是什么?(可以将MongooseJS文档视为类和用作实例的模型吗?)

编辑

这里是JsFiddle中的一个例子,请提供反馈.

//http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
function inheritPrototype(childObject, parentObject) {
    var copyOfParent = Object.create(parentObject.prototype)
    copyOfParent.constructor = childObject
    childObject.prototype = …
Run Code Online (Sandbox Code Playgroud)

javascript oop inheritance mongoose node.js

106
推荐指数
5
解决办法
12万
查看次数

将"虚拟"变量添加到mongoose模式?

我有以下文档架构:

var pageSchema = new Schema({
      name: String
    , desc: String
    , url: String
})
Run Code Online (Sandbox Code Playgroud)

现在,在我的应用程序中,我想在对象内部有页面的html源代码,但我不想将它存储在数据库中.

我应该创建一个"本地"增强对象,它具有对db文档的引用吗?

function Page (docModel, html) {
    this._docModel = docModel
    this._html = html
}
Run Code Online (Sandbox Code Playgroud)

有没有办法通过添加"虚拟"字段直接使用文档模型?

mongoose node.js

21
推荐指数
3
解决办法
3万
查看次数

角度翻译消毒与UTF字符失败

在Angular 1.3.x上使用最新版本的angular-translate.使用$sanitize它似乎直接使用过滤器或服务时会出现问题,但在使用该指令时它会起作用.

建议?

这是一个例子:

var myApp = angular.module('myApp', [ 'pascalprecht.translate', 'ngSanitize' ]);

myApp.config(function($translateProvider) {
    $translateProvider.useSanitizeValueStrategy("sanitize");
    $translateProvider.preferredLanguage('en');
    $translateProvider.translations('en', {
        UTF: 'öéü',
    });
});

myApp.controller("myCtrl", function($scope, $translate) {
    $translate("UTF").then(function(trans) {
        $scope.UTFCTRL = trans;
    });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.16/angular-sanitize.min.js"></script>
<script src="http://rawgit.com/PascalPrecht/bower-angular-translate/master/angular-translate.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
    <div>
        Wrong: <h1>{{ 'UTF' | translate }}</h1>
    </div>
    
    <div>
        Ok: <h1 translate="UTF"></h1>
    </div>
    
    <div>
        Wrong: <h1>{{ UTFCTRL }}</h1>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在jsfiddle:http://jsfiddle.net/gnvpo6aa/

javascript sanitization angularjs angular-translate

17
推荐指数
1
解决办法
8740
查看次数

复制/克隆mongoose文档实例的最简单方法是什么?

我的方法是获取文档实例,并从实例字段中创建一个新实例.我相信有更好的方法可以做到这一点.

document instance mongoose mongodb

16
推荐指数
3
解决办法
2万
查看次数

从任何父级使用ui-router的模态对话框,如何正确指定状态?

我试图打开一个模式对话框采用了棱角分明的UI路由器作为解释在这里.

目标是可以在任何地方访问对话框,不一定需要URL,但如果我可以链接到打开对话框的页面,那将是很好的.

这是破碎的样本:

http://plnkr.co/edit/BLkYME98e3ciK9PQjTh5?p=preview

单击"菜单"应从任一页面打开对话框.

路由逻辑:

app.config(function($stateProvider,$locationProvider, $urlRouterProvider, modalStateProvider) {
  $urlRouterProvider.otherwise("/");
  $locationProvider.html5Mode(true);

  $stateProvider
    .state("app", {
      url: "",
      abstarct: true,
      views: {
        "" : {
          templateUrl: "main.html",
        },
        "header@app": {
          templateUrl: "header.html"
        },
        "footer@app":{
          templateUrl: "footer.html"
        }
      }
    })

    .state("app.home", {
      url: "/",
      templateUrl: "home.html",
    })
    .state("app.content", {
      url: "/content",
      templateUrl: "content1.html",
    });


  modalStateProvider.state("app.home.menu", {
    template: "I am a Dialog!",
    controller: function ($scope) {
      $scope.dismiss = function () {
        $scope.$dismiss();
      };
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

它不应该是"app.home"的孩子,因为我希望它可以从任何地方访问.我怎样才能做到这一点?

angularjs bootstrap-modal angular-ui-router

8
推荐指数
1
解决办法
1万
查看次数

在AngularJS中继续测试之前,量角器没有等待登录重定向,有什么建议吗?

我有一个标准的用户名/密码/提交按钮表单,当用户点击表单提交的按钮时ng-submit="login.submit()",登录和成功使用ui.router($state.go("main"))重定向到主页面.

以下测试失败:

  describe("login", function() {
    beforeEach(function() {
      var email = element(by.model("login.email"));
      email.clear().sendKeys("mail");

      var password =  element(by.model("login.password"));
      password.clear().sendKeys("pass");

      var submit = element(by.id("submit"));
      submit.click();
    });

    it("should be able to login", function() {
      expect(element(by.css(".loginPage")).isPresent()).toBe(false);
      expect(element(by.css(".mainPage")).isPresent()).toBe(true);
    });
  });
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加等待时间,我可以看到浏览器一直停留在登录页面上(点击按钮后) - 然后我得到超时.

成功登录后,浏览器会收到一个带有令牌的cookie,用于验证每个后续请求.

编辑:通过一些修修补补,我发现它失败了..

function login(email, pass) {
  alert("it gets here");
  return _auth.post({ username: email, password: pass }).then(function(data) {
    alert("does not get here");
    console.log("loginok, token:" +$browser.cookies().apiToken); //this should be the received token
    return data;
  });
}
Run Code Online (Sandbox Code Playgroud)

EDIT2:Auth服务

var _auth = Restangular.withConfig(function(Configurer) …
Run Code Online (Sandbox Code Playgroud)

login wait protractor angular-ui-router

7
推荐指数
1
解决办法
4494
查看次数

是否可以在单独的Angular(子)项目之间共享NGXS存储?

情况:我有一个由前端Angular应用程序构成的微型前端,并且将许多Angular库作为模块导入,这些模块包含充当“子应用程序”的组件。

目标:我想在所有子应用程序之间共享在主机应用程序中创建的NGXS存储,以便每个子应用程序都有自己的全局状态片段,并且也可以访问全局状态。

在主机应用程序中,我正在创建状态,如下所示:

@State<ApplicationStateModel>({
  name: 'host',
  defaults: {
    context: {
      language: 'en'
    },
    apps: {}
  }
})
export class ApplicationState {...}
Run Code Online (Sandbox Code Playgroud)

并且在子应用程序中,我希望能够发送动作以及切分此状态,例如:

constructor(private store: Store) {
   // slice the context
   this.context$ = this.store.select(state => state.host.context);
   // slice this sub-app state
   this.state$ = this.store.select(state => state.host.apps['myapp']);
}

...

// dispatch an action
this.store.dispatch(new UpdateContext());
Run Code Online (Sandbox Code Playgroud)

问题:如何将商店从主机应用程序传递到子应用程序?我猜可能有一种方法可以通过在导入过程中使用模块的.forRoot().forFeature()函数来实现。但是我完全迷失了。

store angular ngxs

7
推荐指数
1
解决办法
448
查看次数

如何以平滑的方式根据容器大小自动调整多个JLabel的字体大小?

我需要根据用于调整容器大小的缩放因子来调整多个JLabel的字体大小.为此,我将每个JLabel的字体设置为null,以便它们采用容器的字体.它有效,但它也会产生奇怪的结果.

具体而言,文本似乎"滞后"在容器后面,有时甚至会被截断.我想避免这种行为.知道怎么样?

模拟行为的示例代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.geom.AffineTransform;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TextResize implements Runnable {

    public static void main(String[] args) {
        TextResize example = new TextResize();
        SwingUtilities.invokeLater(example);
    }

    public void run() {
        JFrame frame = new JFrame("JLabel Text Resize");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800, 400));

        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());

        final …
Run Code Online (Sandbox Code Playgroud)

java fonts swing resize jlabel

6
推荐指数
2
解决办法
5785
查看次数

如何构建Node/Angular/Socket.io项目?

我正在开发一个使用AngularJS和Socket.io的项目.我发现了这个非常好的集成示例.

这是项目结构:

app.js                  --> app config
bower.json              --> for bower
package.json            --> for npm
public/                 --> all of the files to be used in on the client side
  css/                  --> css files
    app.css             --> default stylesheet
  img/                  --> image files
  js/                   --> javascript files
    app.js              --> declare top-level app module
    controllers.js      --> application controllers
    directives.js       --> custom angular directives
    filters.js          --> custom angular filters
    services.js         --> custom angular services
  bower_components/
    angular/            --> angular.js
    angular-socket-io/  --> socket.io adapter for …
Run Code Online (Sandbox Code Playgroud)

project node.js socket.io angularjs

6
推荐指数
1
解决办法
2562
查看次数

在angularjs中使用src属性中的ng-repeat变量?

我有以下内容:

div.container
    script(src='/js/bootstrap/holder.js')
    p.text-info(ng-pluralize,
                count="devices.length",
                when="{ '0': 'There are no fragments.', 'one': 'There is 1 fragment.', 'other': 'There are {} fragments.'}")

    ul.unstyled(ng-repeat='fragment in devices')
        ul.thumbnails
            li.span
                div.thumbnail
                    img(src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}")
                    div.caption
                        h3 {{fragment.name}}
                        p {{fragment.dimension}}
                        ul.unstyled(ng-repeat='component in fragment.components')
                            li {{ component.name }}
Run Code Online (Sandbox Code Playgroud)

问题在于src="holder.js/{{fragment.dimension.width}}x{{fragment.dimension.height}}",即使查看生成的html我看到正确的src(src="holder.js/300x200")它也没有显示图像.我猜这不是如何在属性中使用角度变量的.

我怎样才能使它工作?

编辑:它似乎没有执行holder.js..这里是源:在第一次调用我使用角{{hash}}在第二个我手动放holder.js/300x200

<div class="thumbnail">
    <img src="holder.js/1678x638">
    <img src="data:image/png;base64,iVBORw0KG...ElFTkSuQmCC" data-src="holder.js/300x200" alt="300x200" style="width: 300px; height: 200px;">
</div>
Run Code Online (Sandbox Code Playgroud)

variables attributes angularjs angularjs-ng-repeat

6
推荐指数
1
解决办法
6532
查看次数