In Ember.js, what is the best way to display a models properties?
Say my model is 'products' so I have a product list, when I click on an item on that list I want the details displayed in another div and not override that view.
How can I do this, below if my code.
<script type="text/x-handlebars">
{{ view App.ListReleasesView }}
</script>
<script type="text/x-handlebars">
{{ view App.ReleaseDataView }}
</script>
App.ListReleasesView = Ember.View.extend({
templateName: 'app/templates/releases/list',
releasesBinding: 'App.releasesController',
showNew: function() {
this.set('isNewVisible', true);
},
hideNew: function() {
this.set('isNewVisible', false);
},
refreshListing: function() {
App.releasesController.findAll();
}
});
App.selectedReleaseController = Ember.Object.create({
release: null
});
list template:
<ul>
{{#each releases}}
{{view App.ShowReleaseView releaseBinding="this"}}
{{/each}}
</ul>
{{#if isNewVisible}}
{{view App.NewReleaseView}}
{{/if}}
<div class="commands">
<a href="#" {{action "showNew"}}>New Release</a>
<a href="#" {{action "refreshListing"}}>Refresh Listing</a>
</div>
App.ShowReleaseView = Ember.View.extend({
templateName: 'app/templates/releases/show',
//classNames: ['show-release'],
classNameBindings: ['isSelected'],
// tagName: 'tr',
doubleClick: function() {
// this.showEdit();
// this.showRelease();
var release = this.get("release");
App.selectedReleaseController.set("release", release);
},
isSelected: function(){
var selectedItem = App.selectedReleaseController.get("release");
release = this.get("content");
if (release === selectedItem) {return true; }
}.property('App.selectedReleaseController.release')
show template:
{{#if App.selectedReleaseController.release}}
{{view App.ReleaseDataView}}
{{else}}
{{release.name}}
{{/if}}
App.ReleaseDataView = Ember.View.extend({
templateName: 'app/templates/releases/releaseData',
releaseBinding: 'App.selectedReleaseController.release',
// classNames: ['release'],
});
release template:
{{#if isSelected}}
<div class="name">
{{editable "release.name"}} {{editable "release.description"}}
</div>
{{/if}}
You'll want to have a simple controller whose job will be managing the selection state.
App.selectedReleaseController = Ember.Object.create({
content: null
});
Then you'll have another view, for the details, which is bound to that controller.
{{view App.ReleaseDetailsView releaseBinding="App.selectedReleaseController.content"}}
Related
I am building a live search component with Vuejs. The search is working as expected. On keyup the method populates an unordered list. I am at a loss but need to:
Click on a selection from the search results and populate the input with that name.
Get the selected id.
Any suggestions?
The View
<label>Vendor:</label>
<input type="text" v-model="vendor" v-on:keyup="get_vendors">
<div class="panel-footer" v-if="vendors.length">
<ul class="list-group">
<li class="list-group-item for="vendor in vendors">
{{ vendor.vendor_name }}
</li>
</ul>
</div>
The Script
export default {
data: function() {
return {
vendor:'',
vendors: []
}
},
methods: {
get_vendors(){
this.vendors = [];
if(this.vendor.length > 0){
axios.get('search_vendors',{params: {vendor: this.vendor}}).then(response =>
{
this.vendors = response.data;
});
}
}
}
}
</script>
The Route
Route::get('search_vendors', 'vendorController#search_vendors');
The Controller
public function search_vendors(Request $request){
$vendors = vendor::where('vendor_name','LIKE','%'.$request->vendor.'%')->get();
return response()->json($vendors);
}
This is what I came up with. Works nicely.
The View
<label>Vendor:</label>
<input type="text" v-model="vendor" v-on:keyup="get_vendors" class="col-xl-6 form-control ">
<div class="panel-footer autocomplete-box col-xl-6">
<ul class="list-group">
<li v-for="(vendor,id) in vendors" #click="select_vendor(vendor)" class="list-group-item autocomplete-box-li">
{{ vendor.vendor_name }}
</li>
</ul>
</div>
The Script
export default {
data: function() {
return {
vendor:'',
vendor_id:'',
vendors: []
}
},
methods: {
select_vendor(vendor){
this.vendor = vendor.vendor_name
this.vendor_id = vendor.id
this.vendors = [];
},
get_vendors(){
if(this.vendor.length == 0){
this.vendors = [];
}
if(this.vendor.length > 0){
axios.get('search_vendors',{params: {vendor: this.vendor}}).then(response => {
this.vendors = response.data;
});
}
},
},
}
</script>
The Route
Route::get('search_vendors', 'vendorController#search_vendors');
The Controller
public function search_vendors(Request $request){
$vendors = vendor::where('vendor_name','LIKE','%'.$request->vendor.'%')->get();
return response()->json($vendors);
}
The CSS
.autocomplete-box-li:hover {
background-color: #f2f2f2;
}
.autocomplete-box{
position: absolute;
z-index: 1;
}
If you want to get the id of the vendor you can do it using vendor.vendor_id which should be present in the vendor object which is returned from the server and if you want to populate the vendors array with new options you can have a watch or a #change (binding) function to add the entered text field as the new vendor in the vendor array. Also, I would suggest that you download the vue extension as it will help you a lot in debugging
<label>Vendor:</label>
<input type="text" v-model="vendor" v-on:keyup="get_vendors">
<div class="panel-footer" v-if="vendors.length">
<ul class="list-group">
<li class="list-group-item for="(vendor,index) in vendors">
{{ index }}
{{ vendor.vendor_id }}
{{ vendor.vendor_name }}
</li>
</ul>
</div>
I have this accordion with three arrows. When you click on the arrow it should transform 180deg. I want to have a function that takes the parameter set in the function and tells the data property to change to zero.
I've tried
let vm = this;
vm.$data.arrowOne = 0;
And this[arrow] = 0,
And this.arrow = 0
And it's not working.
Here is my code.
<div class="uk-accordion-ekstra">
<ul uk-accordion="multiple: true">
<li>
<a #click="rotate(arrowOne)" class="uk-accordion-title" href="#">Headline
<img :style="{ transform: 'rotate('+ arrowOne + 'deg)'}" src="IMGSRC"></a>
<div class="uk-accordion-content">
<p>TEXT</p>
</div>
</li>
<li>
<a #click="rotate(arrowTwo)" class="uk-accordion-title" href="#">Headline
<img :style="{ transform: 'rotate('+ arrowTwo + 'deg)'}" src="IMGSRC"></a>
<div class="uk-accordion-content">
<p>TEXT</p>
</div>
</li>
<li>
<a #click="rotate(arrowThree)" class="uk-accordion-title" href="#">Headline
<img :style="{ transform: 'rotate('+ arrowThree + 'deg)'}" src="IMGSRC"></a>
<div class="uk-accordion-content">
<p>TEXT</p>
</div>
</li>
</ul>
</div>
And VUE
new Vue({
el: '.uk-accordion-ekstra',
data: {
arrowOne: 180,
arrowTwo: 180,
arrowThree: 180
},
methods: {
arrow: function(arrow) {
if(this.arrow = 0) {
return this.arrow = 180;
}
}
}
});
I have succesfully managed to do it with a switch. But it aren't as beautiful. And im unsure of what i don't get. So help is appreciated.
You have a structural and a scope problem.
You are referencing this.arrow in a method called arrow, and you pass an argument called arrow - try to differentiate with the names, or you can get messed up
You are repeating stuff that you don't need to. Vue is great for creating components for the smallest of elements - you can use it to make your code shorter, more readable and more effective.
The snippet below makes everything easier:
you have one arrow per component, and if you solve the rotation problem in one place, then it's solved everywhere, and the arrow referenced always connects to the component you edit
you can encapsulate your arrow rotation into one component - no need to "litter" the Vue instance with such small animation
I added #click.prevent that's like preventDefault(), so there won't be a jump to the top when you click an <a></a>
Vue.component('accordion', {
props: ['title', 'text'],
template: '<div><a #click.prevent="rotateArrow()" class="uk-accordion-title" href="#">{{ title }} <i class="fa fa-arrow-circle-o-up" :style="`transform: rotate(${rotation}deg)`"></i></a><div class="uk-accordion-content"><p>{{ text }}</p></div></div>',
data() {
return {
rotation: 0
}
},
methods: {
rotateArrow() {
this.rotation = !this.rotation ? 180 : 0
}
}
})
new Vue({
el: '.uk-accordion-ekstra',
data: {
accordionItems: [{
title: 'Headline 1',
text: 'Text 1'
},
{
title: 'Headline 2',
text: 'Text 2'
},
{
title: 'Headline 3',
text: 'Text 3'
},
]
}
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="uk-accordion-ekstra">
<ul uk-accordion="multiple: true">
<li v-for="accordionItem in accordionItems" :key="accordionItem.title">
<accordion :title="accordionItem.title" :text="accordionItem.text"></accordion>
</li>
</ul>
</div>
I have list of 100+ items and rendering takes too much time. I want to show just the once that are visible, and rest on scroll.
What's the best approach?
I have this snippet below, but the vue.set() isn't working.
var dbItems = [{name: 'New item'}, {name:'Another'}, {name:'Third'}];
var app = new Vue({
el: '#app',
data: {
// if I put items : dbItems, then for some reason the Vue.set() doesn't work!!
items : [],
},
methods: {
init: function () {
this.items = dbItems; // we add all items
},
makeItemVisible : function(id) {
console.log("Making visible #"+id);
this.items[id].show = 1;
Vue.set(this.items, id, this.items[id]);
}
}
});
app.init();
app.makeItemVisible(1); // this works
$(document).on('scroll', function(){
// function to show elements when visible
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<div id="app" v-cloak>
<button v-on:click="makeItemVisible(0)">MAKE VISIBLE - This button doesn't work</button>
<div class="items" v-show="items.length">
<!-- I dont know why, but (key, item) had to be switched compared to VUE documentation! -->
<div v-for="(key, item) in items">
<div v-if="item.show" style="border:2px solid green;height:700px">
You can see me: {{ item.name }} | ID: {{ key }}
</div>
<div class="item-blank" data-id="{{ key }}" v-else style="border:2px solid red;height:700px">
{{ item.name }} invisible {{ key }}
</div>
</div>
</div>
</div>
Solved.
Edit: This Vue.js is only useable in Chrome... otherwise it is incredibly slow (Firefox is slowest), it works better when loading the whole document in HTML at once.
var dbItems = [{name: 'New item'}, {name:'Another'}, {name:'Third'}];
var app = new Vue({
el: '#app',
data: {
items : dbItems
},
methods: {
makeItemVisible : function(id) {
console.log("Making visible #"+id);
Vue.set(this.items[id], 'show', 1);
}
}
});
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return (elemTop <= docViewBottom && elemTop >= docViewTop) || (elemBottom >= docViewTop && elemBottom <= docViewBottom);
}
var fn = function(){
$('.item-blank').each(function(){
if(isScrolledIntoView(this)) {
app.makeItemVisible($(this).attr('data-id'));
}
});
};
$(window).scroll(fn);
fn(); // because trigger() doesn't work
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app" v-cloak>
<div class="items" v-show="items.length">
<div v-for="(item, index) in items">
<div v-if="item.show" style="border:2px solid green;height:700px">
You can see me: {{ item.name }} | ID: {{ index }}
</div>
<div class="item-blank" :data-id="index" v-else style="border:2px solid red;height:700px;position:relative;">
{{ item.name }} invisible {{ index }}
</div>
</div>
</div>
</div>
I have the following code and it works by refreshing the page it works fine, but not when clicking on the nav.
How can i achieve that when i click on users, the method fetchData will be executed? So i can see the console.log and the json data.
Navigation:
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<router-link v-for="item in items" v-if="item.navitems && item.navitems.navitem" :to="item.navitems.navitem.url" tag="li" active-class="active" exact><a>{{item.navitems.navitem.name}}</a></router-link>
Content.vue
<template>
<div>
<div class="container">
<h1>{{ slug }}</h1>
<ul v-if="items && items.length">
<li v-for="item of items">
<p><strong>{{item.name}}</strong></p>
<p>{{item.email}}</p>
</li>
</ul>
</div>
</div>
</template>
<script>
export default{
props: ['slug'],
data: () => ({
items: []
}),
created: function () {
this.fetchData();
},
methods: {
fetchData: function () {
var self = this;
console.log(this.slug);
$.get( 'http://jsonplaceholder.typicode.com/' + this.slug, function( data ) {
self.items = data;
console.log(data);
});
}
}
}
</script>
Changed created into mounted and add watch.
watch: {
'$route.params.slug'(newSlug, oldSlug) {
this.fetchData(newId);
}
}
I'm trying to create a common area for my navigation/menus (vertical) in my shell page. This shell's menu area will then be updated by new set of menus on every page transition. heres inside my shell.js
define(function (require) {
var router = require('durandal/plugins/router');
var Menu = function (name, children) {
this.name = ko.observable(name);
this.children = ko.observableArray(children);
}
var Menus = ko.observableArray([]);
function GetDSRList() {
router.navigateTo('#/reports/list2');
}
function activate() {
router.mapNav('reports/index', 'viewmodels/reports/index', 'Reports');
router.mapNav('reports/list', 'viewmodels/reports/list', 'List');
router.mapNav('reports/list2', 'viewmodels/reports/list2', 'List2');
router.mapNav('home/menu', 'viewmodels/home/menu', 'Main');
return router.activate('home/menu');
}
function viewAttached() {
$(document).ready(function () {
$("#panelbar").kendoPanelBar({
expandMode: "single"
}),
$("#splitter").kendoSplitter({
panes: [
{ collapsible: false, resizable: false, size: "20%" },
{ collapsible: false }
]
});
});
}
function token() {
return $("input[name='__RequestVerificationToken']").val();
}
var shell = {
router: router,
activate: activate,
viewAttached: viewAttached,
GetDSRList: GetDSRList,
Menus: Menus,
token: token,
Menu: Menu
}
return shell;
And for the View (shell.html)
<div style="height:100%;">
<div class="loader" data-bind="css: { active: router.isNavigating }">
</div>
<div id="splitter" style="border:0;height:100%">
<div class="nav-panel-section">
<ul id="panelbar">
<li data-bind="foreach : Menus">
<span class="k-link k-header" data-bind="text: name">
<span class="k-icon k-i-arrow-s k-panelbar-expand"></span>
</span>
<ul data-bind="foreach: children">
<li>
<span class="k-link" data-bind="text: $data,click:this.GetDSRList"></span>
</li>
</ul>
</li>
</ul>
</div>
Log off
<div id ="partialContent">
<div class="container-fluid page-host">
<!--ko compose: {
model: router.activeItem, //wiring the router
afterCompose: router.afterCompose, //wiring the router
cacheViews:true //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
}--><!--/ko-->
<form method="post" action="Account/LogOff" id="logoutForm" >
<input type="hidden" name="__RequestVerificationToken" data-bind="value:token"/>
</form>
</div>
</div>
</div>
</div>
Then going to the url '#/reports/list2' i have this viewmodel (list2.js) :
define(function (require) {
var shell = require('viewmodels/home/shell')
function activate() {
shell.Menus = [];
shell.Menus = [
new shell.Menu("Menu3", ["A", "B", "C"]),
new shell.Menu("Menu5", ["A", "B", "C"])
];
}
var list2 = {
activate : activate
}
return list2;
});
But the shell's menu UI does not change unless you refresh the entire page. Am I missing something here? Thnx
Your problem is that you create an observableArray and then wipe it out on the new view model load. You need to use the setter function for your observableArray to maintain it's connection to the DOM (please don't correct my grammar on that one, trying to KISS!!)
function activate() {
shell.Menus = ([]);
shell.Menus = ([
new shell.Menu("Menu3", ["A", "B", "C"]),
new shell.Menu("Menu5", ["A", "B", "C"])
]);
}
Simply adding the parans should fix your issue. When you are clearing use the parans, when you are setting use the parans.