小编Dun*_*can的帖子

淘汰赛:计算可观察与功能

使用knockout时,使用只读计算的observable而不是简单函数有什么好处?

以下是viewmodel构造函数和html代码段,例如:

var ViewModel = function(){
    var self = this;
    self.someProperty = ko.observable("abc");
    self.anotherProperty = ko.observable("xyz");
    self.someComputedProperty = function(){
        return self.someProperty() + self.anotherProperty();
    };    
};

<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty()"></p>
Run Code Online (Sandbox Code Playgroud)

这里的一切似乎都像你期望的那样工作,所以我应该使用以下原因:

?var ViewModel = function(){
    var self = this;
    self.someProperty = ko.observable("abc");
    self.anotherProperty = ko.observable("xyz");
    self.someComputedProperty = ko.computed(function(){
        return self.someProperty() + self.anotherProperty();
    });    
};


<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty"></p>
Run Code Online (Sandbox Code Playgroud)

我注意到http://knockoutjs.com/documentation/computedObservables.html上的文档声明"...声明性绑定只是作为计算的observables实现",所以这是否意味着我需要在我的viewmodels中明确使用它们?

knockout-2.0 knockout.js computed-observable

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

空原型,Object.prototype和Object.create

为什么设置构造函数的prototype属性null不会阻止从该函数创建的对象调用方法Object.prototype,就像设置原型Object.create(null)一样?

也就是说,为什么会这样:

function Foo(){}
Foo.prototype = null;
console.log(new Foo().toString); //outputs function toString() { [native code] } (or whatever)

function Foo(){}
Foo.prototype = Object.create(null);
console.log(new Foo().toString); //output undefined
Run Code Online (Sandbox Code Playgroud)

javascript prototype

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

Angular指令:在隔离范围内使用ng-model

我无法弄清楚如何定义两个自定义指令:

  1. 使用隔离范围,和
  2. 在其模板中的新范围内使用ng-model指令.

这是一个例子:

HTML:

<body ng-app="app">
  <div ng-controller="ctrl">
    <dir model="foo.bar"></dir>
    Outside directive: {{foo.bar}}
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)

JS:

var app = angular.module('app',[])
  .controller('ctrl', function($scope){
    $scope.foo = { bar: 'baz' };
  })
  .directive('dir', function(){
    return {
      restrict: 'E',
      scope: {
        model: '='
      },
      template: '<div ng-if="true"><input type="text" ng-model="model" /><br/></div>'
    }
  });
Run Code Online (Sandbox Code Playgroud)

这里所需的行为是输入的值foo.bar通过指令的(隔离)范围model属性绑定到外部范围的属性.这种情况不会发生,因为模板封闭div上的ng-if指令会创建一个新范围,因此model更新的范围会更新,而不是指令的范围.通常你通过确保表达式中有一个点来解决这些ng模型问题,但我在这里看不到任何方法.我想知道我是否可以使用这样的东西作为我的指令:

{
  restrict: 'E',
  scope: {
    model: {
      value: '=model'
    }
  },
  template: '<div ng-if="true"><input type="text" ng-model="model.value" /><br/></div>'
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用......

Plunker

angularjs angularjs-directive angularjs-scope

10
推荐指数
1
解决办法
2723
查看次数

使用多对多关系更新断开连接的实体

假设我在Entity Framework Code-First设置中有以下模型类:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Team> Teams { get; set; }
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Person> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

从此代码创建的数据库包括一个TeamPersons表,表示人员和团队之间的多对多关系.

现在假设我有一个断开连接的Person对象(不是代理,但尚未附加到上下文),其Teams集合包含一个或多个断开连接的Team对象,所有这些对象都代表已存在于数据库中的团队.例如,如果具有Id 1的Person和具有Id 3的Team已存在于db中,则由以下内容创建的对象:

var person = new Person
{
    Id = 1,
    Name = "Bob",
    Teams = new HashSet<Team>
    {
        new Team …
Run Code Online (Sandbox Code Playgroud)

c# many-to-many entity-framework ef-code-first dbcontext

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

Windows docker 容器内的数据包捕获

是否可以在 Windows docker 容器内捕获 tcp 数据包?(使用最终基于microsoft/windowsservercore的图像)

netsh trace start失败了The inbox capture driver could not be started (error=0x800106d9)

New-NetEventSession -CaptureMode SaveToFile -Name cat -LocalFilePath c:\cat.etl失败了A general error occurred that is not covered by a more specific error code

(上下文 - 我正在尝试调试网络问题,在一段时间后,dockerized 应用程序似乎停止通过已建立的连接接收数据。我可以在 docker 主机上运行跟踪并查看转发到容器的数据;我希望能够在容器内执行相同的操作,看看它是否也出现在那里)

tcp packet-capture docker windows-container

5
推荐指数
0
解决办法
2043
查看次数

Swift通用约束中的元组类型

我试图在Swift中编写一个泛型函数,其约束条件是参数必须是一对数组(我将把它变成一个字典).这可能吗?我已经尝试了以下几种变体,但编译器不喜欢它们中的任何一种.

func foo<K, V, S: SequenceType where S.Generator.Element == (K,V)>(xs: S) { //...}
Run Code Online (Sandbox Code Playgroud)

generics tuples swift

2
推荐指数
1
解决办法
2132
查看次数