Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing an object into data property causes an error. #28

Closed
gearboxdesign opened this issue Oct 11, 2015 · 9 comments
Closed

Passing an object into data property causes an error. #28

gearboxdesign opened this issue Oct 11, 2015 · 9 comments

Comments

@gearboxdesign
Copy link

Hi all,

According to the docs you are able to pass in an object directly into the data attribute, each time I try I seem to get the error:

Warning: undefined is not a function...

Snippet:

data: {
    test: {
        foo: 'bar'
    }
}

Whilst usually I would just pass in a data file in, I have a very specific case in which I need to pass in an object on this occasion.

Any help would be much appreciated...

@doowb
Copy link
Member

doowb commented Oct 12, 2015

Hi @gearboxdesign, which version of grunt-assemble are you using?

@jonschlinkert
Copy link
Member

also, is that data object in a json file? or is it in the grunt config? if it's in a file, what is the name of the file? If you're using it in data.json, then you might be loading that object onto data.data.test etc...

@gearboxdesign
Copy link
Author

Hi guys thanks for the responses, I am currently using 0.4.0, it is in the grunt config, ok so a more complete example, do let me know if I'm going about this a ridiculous way!

So I need to generate a series of index pages which contain links to each files / directories which sit underneath them in a site structure, essentially exactly what a standard server generated index would do.

So I have already written something which, once the initial assemble process has ran to create the static site, scans the output directly creating a tree of paths, so now I recursively run through the tree creating a assemble subtask on the fly in which I want to pass a very specific data object, i.e. the current tree object which pertains to that index... i.e.

'use strict';

var getDirs = require('./getDirs.js'),
    path = require('path'),
    _ = require('lodash');

module.exports = function(grunt) {

    grunt.registerMultiTask('indexer', '.', function() {

        var options = this.options({
            baseDir: null,
            assembleOptions: null
        });

        if(!this.files.length) {
            grunt.log.warn('No files found.');
        }

        this.files.map(function (file) {
            return getDirs(file.src, options);
        }).forEach(_.partial(writeFiles, options));
    });

    function writeFiles (options, content) {

        if (content.paths) {

            grunt.config.set('assemble.indexer-' + content.id, {
                options: _.extend({
                    data: content
                }, options.assembleOptions),
                src: options.template,
                dest: path.join(options.baseDir, content.url, 'index.html')
            });

            grunt.task.run('assemble:indexer-' + content.id);

            content.paths.forEach(_.partial(writeFiles, options));
        }
    }
};

I do wonder a little if all this is necessary, but I was unable to achieve what I wanted with the pages option i.e. gathering a nested structure without extraneous use of helpers, for the above you can assume content.id is a unique id for the task.

the content argument is an object, but even if I try something simple in its place such as an empty object literal or one with test properties such as foo:'bar' I get the same error.

For reference the original default assemble config options are:

options: {
    deploy: grunt.option('deploy'),
    layoutdir: '<%= project.dirs.views.layouts %>',
    partials: [
        '<%= project.dirs.views.elements %>/**/*.hbs',
        '<%= project.dirs.views.components %>/**/*.hbs'
    ]),
    helpers: ['handlebars-helpers'],
    data: '<%= project.dirs.views.data %>/**/*.json'
}

Thanks...

@gearboxdesign
Copy link
Author

Digging a little deeper it would seem that lib/assemble.js line 43 forces any data passed into an array which by the time it reaches tasks/assemble.js line 55 it is assumed the contents are file globs and passing the object through file.expand seems to cause the error... am I trying to do something it simply can't do here?

@burtyish
Copy link

I ran into the same problem.
@jonschlinkert, I think the problem is that we were both using version 0.4.0 (the latest version on npm), where this feature was added only in v0.4.14:

v0.4.14:
date: "2013-10-21"
changes:
- Adds support for plugins running once, before and after (thanks @adjohnson916).
- Adds pagination!
- Thanks to @xzyfer, options.data can now also directly accept an object of data.

See https://github.com/assemble/grunt-assemble/blob/master/CHANGELOG#L28

That's weird. Version 0.4.0 contains changelog for version 0.4.41? how can I get version 0.4.41?

@burtyish
Copy link

Oh, so 0.4.41 refers to a release of the original assemble library, right? I'm confused 😕

@jonschlinkert
Copy link
Member

The changelog is wrong. this library is grunt-assemble. if you do npm install grunt-assemble, that will give you the latest version.

@burtyish
Copy link

Thanks for clearing that up

@jonschlinkert
Copy link
Member

no prob sorry for the confusion

matti142 pushed a commit to matti142/grunt-assemble that referenced this issue Aug 20, 2016
create tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants