If else hogan.js - if-statement

How can i use if / else in hogan. I have following code. Status it is enum, which has two value("locked", "unlocked").I need to check, if status=="locked" then used class="label label-danger" , else class="label label-success". The problem in the syntax, how to do it.
template: [
'<p class="repo-language"><span class="glyphicon glyphicon-user">' +
'</span> <strong>{{fullName}}</strong><span class="pull-right label label-danger">{{status}}</span></p>',
'<p class="repo-name"><small>{{login}} / {{email}}</small></p>'
].join(''),
engine: Hogan

I don't know if it's a good practice, but functions are evaluated, I'm doing conditions like that:
user = {
name: 'foo',
status: 'locked',
statusClass: function() {
if (this.status == 'locked')
return 'danger'
else
return 'success'
}
}
var template = Hogan.compile('<span class="label label-{{user.statusClass}}">{{user.status}}</span>')
alert(template.render({user: user}));
http://jsfiddle.net/WL4QC/

Related

How can I generate access error and error message, leaving user on the page?

In laravel / jquery apps if I need to make checks if user is logged I make in controller:
$loggedUser = Auth::user();
if ( empty($loggedUser->id) ) {
return response()->json(['error_code'=> 1, 'message'=> "You must be logged!"],HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
}
as I do not need to leave the user from the page, but only restrict some functionality
I show error message above using bootstrapGrowl library.
Now with laravel 7 /livewire 1.3 / turbolinks:5 / alpine#v2 I search how can I generate error and
show similar error message, leaving user on the page ?
UPDATED :
Let me explain it with detailed example :
In laravel / jquery apps I have in JS code :
var quiz_quality_radio= $('input[name=quiz_quality_radio]:checked').val()
var href = this_frontend_home_url + "/make-quiz-quality";
$.ajax( {
type: "POST",
dataType: "json",
url: href,
data: {"quiz_quality_id": quiz_quality_radio, "vote_id": this_vote_id, "_token": this_csrf_token},
success: function( response )
{
$('input[name=quiz_quality_radio]:checked').prop('checked', false);
frontendVote.showQuizQualityResults()
popupAlert("Thank you for rating ! Your rate was added!", 'success')
},
error: function( error )
{
$('input[name=quiz_quality_radio]:checked').prop('checked', false);
popupAlert(error.responseJSON.message, 'danger') // 'info', 'success'
}
});
and relative action in control :
public function make_quiz_quality(Request $request)
{
$requestData = $request->all();
$quiz_quality_id = ! empty($requestData['quiz_quality_id']) ? $requestData['quiz_quality_id'] : '';
$vote_id = ! empty($requestData['vote_id']) ? $requestData['vote_id'] : '';
if ( ! Auth::check()) {
return response()->json(['message' => "To rate you must login to the system !"], HTTP_RESPONSE_BAD_REQUEST);
}
if (empty($quiz_quality_id)) {
return response()->json([
'message' => "To rate you must select quiz quality !",
'quiz_quality_id' => $quiz_quality_id
], HTTP_RESPONSE_OK);
}
$vote = Vote::find($vote_id);
if ($vote === null) {
return response()->json([ 'message' => "Vote Item # " . $vote_id . " not found !"],HTTP_RESPONSE_NOT_FOUND);
}
$loggedUser = Auth::user();
$found_count = QuizQualityResult
::getByVoteIdAndUserId($vote_id, $loggedUser->id)
->count();
if ($found_count > 0) {
return response()->json(['message' => "You have already rated '" . $vote->name . "' # vote !", 'vote_id' => $vote_id],
HTTP_RESPONSE_BAD_REQUEST);
}
$newVoteItemUsersResult = new QuizQualityResult();
try {
$newVoteItemUsersResult->quiz_quality_id = $quiz_quality_id;
$newVoteItemUsersResult->vote_id = $vote_id;
$newVoteItemUsersResult->user_id = $loggedUser->id;
DB::beginTransaction();
$newVoteItemUsersResult->save();
DB::commit();
} catch (Exception $e) {
DB::rollBack();
return response()->json(['message' => $e->getMessage(), 'voteCategory' => null], HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
}
return response()->json(['message' => '', 'id' => $newVoteItemUsersResult->id], HTTP_RESPONSE_OK_RESOURCE_CREATED);
} // public function make_quiz_quality(Request $request)
and in case of error generated in error block I show message with function popupAlert
(implemented with bootstrapGrowl), without leaving the page.
That is what I want to make in livewire / turbolinks / alpine app. How can I do it?
UPDATED # 2:
That is just listing of items user can vote for:
<div class="table-responsive">
<table class="table text-primary">
#foreach($quizQualityOptions as $key=>$next_quiz_quality_option)
<tr>
<td>
<input class="" type="radio" name="quiz_quality_radio" id="quiz_quality_radio_{{ $next_quiz_quality_option }}" value="{{ $key }}">
<label class="col-form-label" for="quiz_quality_radio_{{ $next_quiz_quality_option }}">{{ $next_quiz_quality_option }}</label>
</td>
</tr>
#endforeach
</table>
</div>
<div class="row p-3">
<a class="btn btn-primary a_link" onclick="javascript:frontendVote.MakeQuizQuality()">Rate !</a>
</div>
with 2 restrictions :
User must be logged
Any logged user can vote only once
these 2 errors were genarated at server.
UPDATED # 3:
I found decision with using of axios, like :
<button type="submit" class="btn btn-primary btn-sm m-2 ml-4 mr-4 action_link" #click.prevent="submitNewTodo()">
Submit
</button>
submitNewTodo() {
console.log('submitNewTodo::')
let is_insert= 1
let current_toto_id= 1
axios({
method: (is_insert ? 'post' : 'patch'),
url: '/api/todos' + (!is_insert ? "/" + current_toto_id : ''),
data: {
text : this.new_todo_text,
priority : this.new_todo_priority
},
}).then((response) => {
this.new_todo_text= ''
this.new_todo_priority= ''
this.loadTodosRows()
popupAlert( 'Todo ' + (is_insert ? 'added' : 'updated') + ' successfully !', 'success' )
}).catch((error) => {
var validationErrors= convertObjectToArray(error.response.data.errors.text)
this.validation_errors_text= ''
validationErrors.map((next_error, index) => {
if(next_error && typeof next_error[1] != "undefined" ) {
this.validation_errors_text += '<li>'+next_error[1]+'</li>'
}
})
popupErrorMessage(error.response.data.message)
});
},
With it I show message both on success and failure as I need but I see big disadvantage with it
as I use livewire and I would like to use livewire here, if that is possible...
Hope I explained what I want clearly...
Thanks!
With Alpine.js and axios you could do something like this, note that I'm not sure whether or not this_frontend_home_url, this_vote_id and this_csrf_token will be defined.
<div x-data="quiz()">
<div>
<div class="table-responsive">
<table class="table text-primary">
#foreach($quizQualityOptions as $key=>$next_quiz_quality_option)
<tr>
<td>
<input x-model="selected_quiz" class="" type="radio" name="quiz_quality_radio"
id="quiz_quality_radio_{{ $next_quiz_quality_option }}" value="{{ $key }}">
<label class="col-form-label"
for="quiz_quality_radio_{{ $next_quiz_quality_option }}">{{ $next_quiz_quality_option }}</label>
</td>
</tr>
#endforeach
</table>
</div>
<div class="row p-3">
<a class="btn btn-primary a_link" #click="submitQuizQuality()">Rate !</a>
</div>
</div>
</div>
<script>
function quiz() {
return {
selected_quiz: null,
submitQuizQuality() {
const url = this_frontend_home_url + "/make-quiz-quality";
axios.post(url, {
quiz_quality_id: this.selected_quiz,
vote_id: this_vote_id, // no idea where this is coming from,
_token: this_csrf_token // no idea where this is coming from
}).then(() => {
// reset "checked" state
this.selected_quiz = null;
frontendVote.showQuizQualityResults();
popupAlert("Thank you for rating ! Your rate was added!", 'success');
}).catch(error => {
// reset "checked" state
this.selected_quiz = null;
if (error && error.response) {
popupAlert(error.response.message, 'danger')
}
});
}
}
}
</script>

Comparing value from html to context

I need to compare a value (variable) extracted from a page to context.
For example:
Color is a dropdown selection.
$(document).ready(function(){
$("#color").change(function() {
var selected_color = $(this).val() ;
{% if context_value == selected_color %}
.. do something
{% endif %}
})};
Is this possible ? If not, is there some solution for such case ?
I recommend you use Ajax to communicate asynchronously between JavaScript and python (without refreshing the page).
your JS:
$(document).ready(function(){
$("#color").change(function() {
var selected_color = $(this).val() ;
$.ajax({
method: "POST",
url: 'color_check',
data: selected_color,
success: handleFormSuccess,
error: handleFormError,
})
})
function handleFormSuccess(data, textStatus, jqXHR){
.. do something
}
function handleFormError(jqXHR, textStatus, errorThrown){}
};
Your python view:
def color_check(request):
if request.is_ajax():
selected_color = request.POST
context_value = 'Red'
if selected_color == context_value:
return JsonResponse(True)
EDIT: Arun Singh's solution is simpler and works too. I would only make the paragraph hidden from the user:
<p style="display:none" id="my-data" data-name="{{context_value}}"></p>
<p id='data'>{{ context_value }}</p>
or
<p id="my-data" data-name="{{context_value}}"></p>
$(document).ready(function(){
$("#color").change(function() {
var selected_color = $(this).val() ;
var djangoData = $('#data').val();
if (djangoData === selected_color){
console.log('do something)
}else{
console.log('do something else')
}
})};

Opencart if product available Button show else disable that button

Hi i'm creating a application using Opencart. It fully customized, i have doubt in this.
I have filter.tpl page, in this page i need to display and hide button based on product availability
Eg:
If product available show like this
enter image description here
else button show like this enter image description here
Am trying this fowling code using ajax
filter.tpl
$('input[name=\'filter_name\']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/getProductCheck' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['product_id']
}
}));
}
});
},
'select': function(item) {
$('input[name=\'filter_name\']').val(item['label']);
}
});
In controller
product.php
public function getProductCheck()
{
/*Some code here*/
}
So you can use if ($product['quantity']) statement for example
I got the out put am using javascript following code
<div class="form-group">
<div style='display:none;' id='instock'>
<a class='instock-btn'>Product / Solution Available</a>
<input type='submit' class='btn-orng available' name='' value="Click here for more details" size='20' />
</div>
<div style='display:none;' id="outstock">
<input type='submit' class='outstock-btn' name='' value="Product / Solution Not Available" size='20' />
<input type='submit' class='btn-orng' name='' value="We will contact you at the earliest" size='20' />
</div>
</div>
script
$(document).ready(function(){
$('#dia1').on('change', function() {
//var value =
if (this.value <='320' )
{
$("#instock").show();
$("#outstock").hide();
}
else
{
$("#instock").hide();
$("#outstock").show();
}
});
$('#len1').on('change', function() {
//var value =
if (this.value <='310' )
{
$("#instock").show();
$("#outstock").hide();
}
else
{
$("#instock").hide();
$("#outstock").show();
}
});
});

dom-if does not update according to the condition

I'm trying to build a simple login system and I have 2 different templates in it: One when the user is not logged in yet (which displays a "sign in" button), and one when the user is logged in (which displays the username)
By default, the first one is displayed. But when the user is logged in, my first template is not destroyed and my second template is not displayed.
<template is="dom-if" if="{{!logged}}" restamp="true">
<div class="box" id="notLogged">
<paper-button class="loginButton" on-tap="loginPopup"><iron-icon class="avatar" icon="account-circle"></iron-icon><span id="notLoggedMessage">Sign in</span></paper-button>
</div>
</template>
<template is="dom-if" if="{{logged}}" restamp="true">
<div class="box" id="logged">
<paper-button class="loginButton" on-tap="logoutPopup">
<img src="https://placehold.it/40x40" alt="user avatar" />
</paper-button>
</div>
</template>
and now the script. As you can see, I don't use any ajax yet because the service is not done yet. So I'm faking it with "loginOk" value
Polymer({
is: 'system-login',
properties: {
logged: {
type: Boolean,
value: false}
},
loginPopup: function (e) {
loginWindow.open();
},
logoutPopup: function (e) {
logoutWindow.open();
},
checkLogin: function () {
var loginOk = 1;
if (loginOk === 1) {
this.logged === true;
loginWindow.close();
} else if (loginOk === 2) {
errorMessage.style.display = "inline";
} else {
return;
}
}
});
The problem is this.logged === true;. === in JavaScript is a comparison operator and not an assignment operator. So what your code does is it compares the value and type of this.true with Boolean true and returns false (which you are not catching).
Changing it to this.logged = true should do the trick

confused with Ember arrangedContent, object with id > 100 not paginating correctly

on a crusade to learn ember, I'm trying to create a blog with it, and now I'm on the pagination step.
Everything is working, except all of my posts with an id of over 100 don't show up in the beginning.
This is what my PostsIndexController looks like ->
Blog.PostsIndexController = Ember.ArrayController.extend({
sortProperties: ['id'],
sortAscending: false,
page: 1,
perPage: 8,
totalPages: (function() {
return Math.ceil(this.get('length') / this.get('perPage'));
}).property('length', 'perPage'),
pages: (function() {
var collection = Ember.A();
for(var i = 0; i < this.get('totalPages'); i++) {
collection.pushObject(Ember.Object.create({
number: i + 1
}));
}
return collection;
}).property('totalPages'),
hasPages: (function() {
return this.get('totalPages') > 1;
}).property('totalPages'),
prevPage: (function() {
var page = this.get('page');
var totalPages = this.get('totalPages');
if(page > 1 && totalPages > 1) {
return page - 1;
} else {
return null;
}
}).property('page', 'totalPages'),
nextPage: (function() {
var page = this.get('page');
var totalPages = this.get('totalPages');
if(page < totalPages && totalPages > 1) {
return page + 1;
} else {
return null;
}
}).property('page', 'totalPages'),
paginatedContent: (function() {
var start = (this.get('page') - 1) * this.get('perPage');
var end = start + this.get('perPage');
return this.get('arrangedContent').slice(start, end);
}).property('page', 'totalPages', 'arrangedContent.[]'),
selectPage: function(number) {
this.set('page', number);
}
});
and this is what my template looks like ->
{{#each post in paginatedContent}}
{{ render 'posts/post' post}}
{{/each}}
{{#if hasPages}}
<div class="pagination">
{{#if prevPage}}
<a href="#" class="previous_page" rel="previous" {{action "selectPage" prevPage}}>← Newer</a>
{{else}}
<span class="previous_page disabled">← Newer</span>
{{/if}}
{{#if nextPage}}
<a href="#" class="next_page" rel="next" {{action "selectPage" nextPage}}>Older →</a>
{{else}}
<span class="next_page disabled">Older →</span>
{{/if}}
</div>
{{/if}}
I think the problem is in the way arrangedContent is setting up the array --
paginatedContent: (function() {
var start = (this.get('page') - 1) * this.get('perPage');
var end = start + this.get('perPage');
return this.get('arrangedContent').slice(start, end);
}).property('page', 'totalPages', 'arrangedContent.[]'),
But, am a bit confused with what arrangedContent is, and how to fix this issue. Help much appreciated!
I Haven't quite figured out why this happened, but instead of sorting by id, I just sorted by created_at, which fixed the issue.
sortProperties: ['created_at'],
You can create an other "real" numeric type ID for the model, and order by this field. Works fine!
App.Msgboard = DS.Model.extend({
numericId: function(){
var id = this.get('id');
if (id) { return +id; }
}.property('id'),
name: DS.attr('string')
});