Skip to content

Commit

Permalink
Merge pull request #73 from canjs/major
Browse files Browse the repository at this point in the history
DONT MERGE: Major
  • Loading branch information
matthewp authored Jan 29, 2018
2 parents 0684ce0 + 4c97606 commit dc5dcb6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 55 deletions.
18 changes: 0 additions & 18 deletions build.js

This file was deleted.

32 changes: 20 additions & 12 deletions can-view-import.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
var assign = require('can-assign');
var canData = require('can-dom-data-state');
var canSymbol = require('can-symbol');
var DOCUMENT = require("can-globals/document/document");
var getChildNodes = require('can-util/dom/child-nodes/child-nodes');
var importer = require('can-util/js/import/import');
var mutate = require("can-util/dom/mutate/mutate");
var domMutate = require('can-dom-mutate');
var domMutateNode = require("can-dom-mutate/node");
var nodeLists = require('can-view-nodelist');
var viewCallbacks = require('can-view-callbacks');
var tag = viewCallbacks.tag;
var events = require('can-event');
var canLog = require("can-log/");
var dev = require("can-log/dev/dev");

function setViewModel (element, viewModel) {
element[canSymbol.for('can.viewModel')] = viewModel;
}

function processImport(el, tagData) {

var moduleName = el.getAttribute("from");
// If the module is part of the helpers pass that into can.import
// as the parentName
var templateModule = tagData.options.get("helpers.module");
var templateModule = tagData.scope.get("scope.helpers.module");
var parentName = templateModule ? templateModule.id : undefined;

if(!moduleName) {
Expand All @@ -29,7 +34,7 @@ function processImport(el, tagData) {
});

// Set the viewModel to the promise
canData.set.call(el, "viewModel", importPromise);
setViewModel(el, importPromise);
canData.set.call(el, "scope", importPromise);

// Set the scope
Expand All @@ -53,24 +58,27 @@ function processImport(el, tagData) {
}));
canData.set.call(el, "preventDataBindings", false);

canData.set.call(el, "viewModel", importPromise);
setViewModel(el, importPromise);
canData.set.call(el, "scope", importPromise);
}
}
// Render the subtemplate and register nodeLists
else {
var nodeList = nodeLists.register([], undefined, tagData.parentNodeList || true, false);
nodeList.expression = "<" + this.tagName + ">";

var frag = tagData.subtemplate ?
tagData.subtemplate(scope, tagData.options) :
tagData.subtemplate(scope, tagData.options, nodeList) :
DOCUMENT().createDocumentFragment();

var nodeList = nodeLists.register([], undefined, tagData.parentNodeList || true);
nodeList.expression = "<" + this.tagName + ">";

events.one.call(el, "removed", function(){
nodeLists.unregister(nodeList);
var removalDisposal = domMutate.onNodeRemoval(el, function () {
if (!el.ownerDocument.contains(el)) {
removalDisposal();
nodeLists.unregister(nodeList);
}
});

mutate.appendChild.call(el, frag);
domMutateNode.appendChild.call(el, frag);
nodeLists.update(nodeList, getChildNodes(el));
}
}
Expand Down
52 changes: 44 additions & 8 deletions can-view-import_test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var CanMap = require('can-map');
var Component = require('can-component');
var SimpleMap = require('can-simple-map');
var stache = require('can-stache');
var compute = require('can-compute');
var getIntermediateAndImports = require('can-stache/src/intermediate_and_imports');
var QUnit = require('steal-qunit');
var importer = require('can-util/js/import/import');
var tag = require('can-view-callbacks').tag;
var testHelpers = require('can-test-helpers');
var SimpleObservable = require("can-simple-observable");

require('./can-view-import');

Expand Down Expand Up @@ -38,11 +37,11 @@ if(window.steal) {
"{{#eq state 'resolved'}}<hello-world></hello-world>{{/eq}}</can-import>{{/if a}}");
var template = stache(iai.intermediate);

var a = compute(false);
var a = new SimpleObservable(false);
var res = template({ a: a });

equal(res.childNodes[0].childNodes.length, 0, "There are no child nodes immediately");
a(true);
a.set(true);

importer("can-view-import/test/hello").then(function(){
equal(res.childNodes[0].childNodes.length, 1, "There is now a nested component");
Expand Down Expand Up @@ -91,22 +90,58 @@ if(window.steal) {
finishWarningCheck();
});

if (!System.isEnv('production')) {
/*asyncTest("nodeLists are properly handed down", function(){
expect(1);
var templateString = "{{#if(map.render)}}<can-import from='can-view-import/test/hello'>" +
"{{#if isResolved}}{{#with scope.root}}{{#if(map.show)}}{{foo}}{{/if}}" +
"{{/with}}{{/if}}</can-import>{{/if}}";
var iai = getIntermediateAndImports(templateString);
var template = stache(iai.intermediate);
var map = new SimpleMap({
render: true,
show: true,
bar: "bar"
});
var count = 0;
var foo = new Observation(function(){
count++;
equal(count, 1, "This was called too many times");
return map.get("bar");
});
template({ foo: foo, map: map });
importer("can-view-import/test/hello").then(function(){
// Get around temporary bind stuff
setTimeout(function(){
queues.batch.start();
map.set("show", false);
map.set("bar", undefined);
queues.batch.stop();
start();
}, 100);
});
});*/
}

if (!System.isEnv('production')) {
asyncTest("can use an import's value", function(){
var template = "<can-import from='can-view-import/test/person' @value:to='*person' />hello {{*person.name}}";

var iai = getIntermediateAndImports(template);

var renderer = stache(iai.intermediate);
var res = renderer(new CanMap());
var res = renderer(new SimpleMap());

importer("can-view-import/test/person").then(function(){
equal(res.childNodes[2].nodeValue, "world", "Got the person.name from the import");
start();
});
});
}

/*
if (!System.isEnv('production')) {
asyncTest("can import a template and use it", function(){
var template = "<can-import from='can-view-import/test/other.stache!' @value:to='*other' />{{{*other()}}}";
Expand Down Expand Up @@ -160,7 +195,7 @@ if(window.steal) {
});
stache.async(template).then(function(renderer){
var frag = renderer(new CanMap());
var frag = renderer(new SimpleMap());
finishWarningCheck();
Expand Down Expand Up @@ -238,4 +273,5 @@ if(window.steal) {
});
});
}
*/
}
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "can-view-import",
"version": "3.2.8",
"version": "4.0.0-pre.10",
"description": "Import dependencies in CanJS views",
"homepage": "https://canjs.com",
"homepage": "https://canjs.com/doc/can-view-import.html",
"repository": {
"type": "git",
"url": "git://github.com/canjs/can-view-import.git"
Expand All @@ -13,44 +13,44 @@
"url": "http://bitovi.com"
},
"scripts": {
"preversion": "npm test && npm run build",
"version": "git commit -am \"Update dist for release\" && git checkout -b release && git add -f dist/",
"postversion": "git push --tags && git checkout master && git branch -D release && git push",
"preversion": "npm test",
"postpublish": "git push --tags && git push",
"testee": "testee test/test.html --browsers firefox",
"test": "npm run detect-cycle && npm run jshint && npm run testee",
"jshint": "jshint ./*.js --config",
"release:pre": "npm version prerelease && npm publish --tag=pre",
"release:patch": "npm version patch && npm publish",
"release:minor": "npm version minor && npm publish",
"release:major": "npm version major && npm publish",
"build": "node build.js",
"detect-cycle": "detect-cyclic-packages --ignore done-serve,steal-stache"
},
"main": "can-view-import",
"keywords": [],
"dependencies": {
"can-assign": "^1.0.0",
"can-dom-data-state": "^0.2.0",
"can-event": "^3.6.0",
"can-dom-data-state": "<2.0.0",
"can-dom-mutate": "<2.0.0",
"can-globals": "<2.0.0",
"can-symbol": "^1.5.0",
"can-log": "^1.0.0",
"can-stache": "^3.7.3",
"can-util": "^3.9.5",
"can-view-callbacks": "^3.2.0",
"can-view-nodelist": "^3.1.0"
"can-view-callbacks": "^4.0.0",
"can-view-nodelist": "^4.0.0"
},
"devDependencies": {
"bit-docs": "0.0.7",
"can-component": "^3.2.2",
"can-compute": "^3.3.1",
"can-map": "^3.3.1",
"can-stache": "^3.3.0",
"can-component": "^4.0.0",
"can-observation": "4.0.0",
"can-queues": "<2.0.0",
"can-simple-map": "^4.0.0",
"can-simple-observable": "^2.0.0",
"can-stache": "^4.0.0",
"can-test-helpers": "^1.0.1",
"detect-cyclic-packages": "^1.1.0",
"jshint": "^2.9.1",
"steal": "^1.2.0",
"steal-qunit": "^1.0.0",
"steal-stache": "^3.1.0",
"steal-stache": "^4.0.0-pre.1",
"steal-tools": "^1.0.0",
"testee": "^0.7.0"
}
Expand Down
2 changes: 1 addition & 1 deletion test/person.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["can-map"], function(CanMap){
define(["can-simple-map"], function(CanMap){

return new CanMap({
name: "world"
Expand Down

0 comments on commit dc5dcb6

Please sign in to comment.