Permalink
Browse files

adicionando smartObservableArray.js

  • Loading branch information...
1 parent fe8e6d3 commit 759ea9a2ad4667336469883e7d6d7e918b1426a1 @herberthamaral committed Dec 31, 2011
Showing with 3,430 additions and 2 deletions.
  1. +2 −1 index.html
  2. +15 −1 lib/experiment.js
  3. +27 −0 lib/knockout-entities.js
  4. +3,223 −0 lib/knockout-latest.debug.js
  5. +135 −0 lib/smartObservableArray.js
  6. +28 −0 spec/knockout-entities-test.js
View
@@ -23,7 +23,8 @@
</form>
<script src="lib/jquery-1.7.1.min.js"></script>
<script src="lib/jquery.tmpl.js"></script>
- <script src="lib/knockout-2.0.0.js" ></script>
+ <script src="lib/knockout-latest.debug.js" ></script>
+ <script src="lib/smartObservableArray.js" ></script>
<script type="text/html" id="listaPessoas">
<li>
View
@@ -1,4 +1,4 @@
-var PessoaRepository = ko.observableArray();
+var PessoaRepository = ko.smartObservableArray();
var Pessoa = function(nome, sobrenome){
this.nome = ko.observable(nome);
@@ -31,6 +31,7 @@ var PessoaItemListViewModel = function(pessoa){
});
}
+
var PessoasListViewModel = {
list : ko.observableArray(),
push: function(itemListViewModel){
@@ -49,6 +50,19 @@ var PessoasListViewModel = {
}
+PessoaRepository.subscribe(function(newValue){
+ var pessoaItemList = new PessoaItemListViewModel(newValue);
+ PessoasListViewModel.list.push(pessoaItemList);
+}, null, "smartAdded");
+
+PessoaRepository.subscribe(function(value){
+ var len = PessoasListViewModel.length;
+ for(var i=0; i<len; i++)
+ if (PessoasListViewModel.list()[i].pessoa==value)
+ PessoasListViewModel.list.remove(PessoasListViewModel.list()[i]);
+}, null, "smartRemoved");
+
+
var PessoaFormViewModel = function(){
this.habilitado = ko.observable(true);
this.nome = ko.observable('Herp');
View
@@ -0,0 +1,27 @@
+function knockoutEntity(object){
+ var KoEntity = function(){
+ this._properties = [];
+ for(entry in object){
+ this[entry] = object[entry];
+ this._properties.push(entry);
+ }
+ this.toJS = function(){
+ var json = {};
+ for(var i=0; i<this._properties.length; i++){
+ var property = this[this._properties[i]];
+ if(typeof(property) === "function")
+ json[this._properties[i]] = property();
+ else
+ json[this._properties[i]] = property;
+ }
+
+ return json;
+ }
+ }
+ KoEntity.addExtraProperty = function(property){
+ for(entry in property){
+ KoEntity.prototype[entry] = property[entry];
+ }
+ };
+ return KoEntity;
+}
Oops, something went wrong.

0 comments on commit 759ea9a

Please sign in to comment.