I’m not able to get a very simple example of an autocomplete component to run. It seems that the “app.autocomplete” object is always null. Given the following scenario I always get:
Uncaught TypeError: Cannot read property ‘create’ of undefined
I’m hoping given the info below someone can help me put my finger on what I’m missing…
This is my first F7 app. I’ve used a simple blank f7 core template and added an autocomplete component like so:
I’ve also added the corresponding sample js code to the section at the bottom of the html page:
<script>
// Fruits data demo array
var fruits = ('Apple Apricot Avocado Banana Melon Orange Peach Pear Pineapple').split(' ');
var autocompleteDropdownSimple = app.autocomplete.create({
inputEl: '#autocomplete-dropdown',
openIn: 'dropdown',
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
export default { }
</script>
Now when the page renders in the browser, the app.autocomplete is always undefined so the create call always fails.
home.f7.html:4 Uncaught TypeError: Cannot read property ‘create’ of undefined
at eval (home.f7.html:4)
at Module…/src/pages/home.f7.html (app.js:4840)
at webpack_require (app.js:790)
at fn (app.js:101)
at eval (routes.js:2)
at Module…/src/js/routes.js (app.js:4828)
at webpack_require (app.js:790)
at fn (app.js:101)
at eval (app.js:11)
at Module…/src/js/app.js (app.js:4804)
I’m sure I’m missing something simple but I can’t find it. Any help would be greatly appreciated.
you are mixing concepts.
try using f7 components, then it should work
this code is just an example taken from the docs. also look the kitchen sink app to know how f7 works.
routes = [
// ...
{
path: '/some-page/',
// Component Object
component: {
template: `
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner">
<div class="title">{{title}}</div>
</div>
</div>
<div class="page-content">
<a @click="openAlert" class="red-link">Open Alert</a>
<div class="list">
<ul>
<li class="item-content item-input inline-label">
<div class="item-inner">
<div class="item-title item-label">Fruit</div>
<div class="item-input-wrap">
<input id="autocomplete-dropdown" type="text" placeholder="Fruit">
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
`,
style: `
.red-link {
color: red;
}
`,
data: function () {
return {
title: 'Component Page',
names: ['John', 'Vladimir', 'Timo'],
}
},
methods: {
openAlert: function () {
var self = this;
self.$app.dialog.alert('Hello world!');
},
},
on: {
pageInit: function (e, page) {
// do something on page init
// Fruits data demo array
const app = page.app
var fruits = ('Apple Apricot Avocado Banana Melon Orange Peach Pear Pineapple').split(' ');
var autocompleteDropdownSimple = app.autocomplete.create({
inputEl: '#autocomplete-dropdown',
openIn: 'dropdown',
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
},
}
},
},
]